dhcp4_srv_unittest.cc 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591
  1. // Copyright (C) 2011-2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <sstream>
  16. #include <asiolink/io_address.h>
  17. #include <config/ccsession.h>
  18. #include <dhcp4/tests/dhcp4_test_utils.h>
  19. #include <dhcp/dhcp4.h>
  20. #include <dhcp/iface_mgr.h>
  21. #include <dhcp/option.h>
  22. #include <dhcp/option_int.h>
  23. #include <dhcp/option4_addrlst.h>
  24. #include <dhcp/option_custom.h>
  25. #include <dhcp/option_int_array.h>
  26. #include <dhcp/option_vendor.h>
  27. #include <dhcp/pkt_filter.h>
  28. #include <dhcp/pkt_filter_inet.h>
  29. #include <dhcp/docsis3_option_defs.h>
  30. #include <dhcp/tests/iface_mgr_test_config.h>
  31. #include <dhcp4/dhcp4_srv.h>
  32. #include <dhcp4/dhcp4_log.h>
  33. #include <dhcp4/json_config_parser.h>
  34. #include <hooks/server_hooks.h>
  35. #include <dhcpsrv/cfgmgr.h>
  36. #include <dhcpsrv/lease_mgr.h>
  37. #include <dhcpsrv/lease_mgr_factory.h>
  38. #include <dhcpsrv/utils.h>
  39. #include <gtest/gtest.h>
  40. #include <hooks/server_hooks.h>
  41. #include <hooks/hooks_manager.h>
  42. #include <config/ccsession.h>
  43. #include <boost/scoped_ptr.hpp>
  44. #include <iostream>
  45. #include <arpa/inet.h>
  46. using namespace std;
  47. using namespace isc;
  48. using namespace isc::dhcp;
  49. using namespace isc::data;
  50. using namespace isc::asiolink;
  51. using namespace isc::hooks;
  52. using namespace isc::dhcp::test;
  53. namespace {
  54. // This test verifies that the destination address of the response
  55. // message is set to giaddr, when giaddr is set to non-zero address
  56. // in the received message.
  57. TEST_F(Dhcpv4SrvTest, adjustIfaceDataRelay) {
  58. IfaceMgrTestConfig test_config(true);
  59. IfaceMgr::instance().openSockets4();
  60. // Create the instance of the incoming packet.
  61. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
  62. // Set the giaddr to non-zero address and hops to non-zero value
  63. // as if it was relayed.
  64. req->setGiaddr(IOAddress("192.0.1.1"));
  65. req->setHops(2);
  66. // Set ciaddr to zero. This simulates the client which applies
  67. // for the new lease.
  68. req->setCiaddr(IOAddress("0.0.0.0"));
  69. // Clear broadcast flag.
  70. req->setFlags(0x0000);
  71. // Set local address, port and interface.
  72. req->setLocalAddr(IOAddress("192.0.2.1"));
  73. req->setLocalPort(1001);
  74. req->setIface("eth1");
  75. req->setIndex(1);
  76. // Create a response packet. Assume that the new lease have
  77. // been created and new address allocated. This address is
  78. // stored in yiaddr field.
  79. boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
  80. resp->setYiaddr(IOAddress("192.0.1.100"));
  81. // Clear the remote address.
  82. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  83. // Set hops value for the response.
  84. resp->setHops(req->getHops());
  85. // This function never throws.
  86. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
  87. // Now the destination address should be relay's address.
  88. EXPECT_EQ("192.0.1.1", resp->getRemoteAddr().toText());
  89. // The query has been relayed, so the response must be sent to the port 67.
  90. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getRemotePort());
  91. // Local address should be the address assigned to interface eth1.
  92. EXPECT_EQ("192.0.2.3", resp->getLocalAddr().toText());
  93. // The local port is always DHCPv4 server port 67.
  94. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
  95. // We will send response over the same interface which was used to receive
  96. // query.
  97. EXPECT_EQ("eth1", resp->getIface());
  98. EXPECT_EQ(1, resp->getIndex());
  99. // Let's do another test and set other fields: ciaddr and
  100. // flags. By doing it, we want to make sure that the relay
  101. // address will take precedence.
  102. req->setGiaddr(IOAddress("192.0.1.50"));
  103. req->setCiaddr(IOAddress("192.0.1.11"));
  104. req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
  105. resp->setYiaddr(IOAddress("192.0.1.100"));
  106. // Clear remote address.
  107. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  108. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
  109. // Response should be sent back to the relay address.
  110. EXPECT_EQ("192.0.1.50", resp->getRemoteAddr().toText());
  111. }
  112. // This test verifies that the destination address of the response message
  113. // is set to ciaddr when giaddr is set to zero and the ciaddr is set to
  114. // non-zero address in the received message. This is the case when the
  115. // client is in Renew or Rebind state.
  116. TEST_F(Dhcpv4SrvTest, adjustIfaceDataRenew) {
  117. IfaceMgrTestConfig test_config(true);
  118. IfaceMgr::instance().openSockets4();
  119. // Create instance of the incoming packet.
  120. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
  121. // Clear giaddr to simulate direct packet.
  122. req->setGiaddr(IOAddress("0.0.0.0"));
  123. // Set ciaddr to non-zero address. The response should be sent to this
  124. // address as the client is in renewing or rebinding state (it is fully
  125. // configured).
  126. req->setCiaddr(IOAddress("192.0.1.15"));
  127. // Let's configure broadcast flag. It should be ignored because
  128. // we are responding directly to the client having an address
  129. // and trying to extend his lease. Broadcast flag is only used
  130. // when new lease is acquired and server must make a decision
  131. // whether to unicast the response to the acquired address or
  132. // broadcast it.
  133. req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
  134. // This is a direct message, so the hops should be cleared.
  135. req->setHops(0);
  136. // Set local unicast address as if we are renewing a lease.
  137. req->setLocalAddr(IOAddress("192.0.2.1"));
  138. // Request is received on the DHCPv4 server port.
  139. req->setLocalPort(DHCP4_SERVER_PORT);
  140. // Set the interface. The response should be sent over the same interface.
  141. req->setIface("eth1");
  142. req->setIndex(1);
  143. // Create a response.
  144. boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
  145. // Let's extend the lease for the client in such a way that
  146. // it will actually get different address. The response
  147. // should not be sent to this address but rather to ciaddr
  148. // as client still have ciaddr configured.
  149. resp->setYiaddr(IOAddress("192.0.1.13"));
  150. // Clear the remote address.
  151. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  152. // Copy hops value from the query.
  153. resp->setHops(req->getHops());
  154. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
  155. // Check that server responds to ciaddr
  156. EXPECT_EQ("192.0.1.15", resp->getRemoteAddr().toText());
  157. // The query was non-relayed, so the response should be sent to a DHCPv4
  158. // client port 68.
  159. EXPECT_EQ(DHCP4_CLIENT_PORT, resp->getRemotePort());
  160. // The response should be sent from the unicast address on which the
  161. // query has been received.
  162. EXPECT_EQ("192.0.2.3", resp->getLocalAddr().toText());
  163. // The response should be sent from the DHCPv4 server port.
  164. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
  165. // The interface data should match the data in the query.
  166. EXPECT_EQ("eth1", resp->getIface());
  167. EXPECT_EQ(1, resp->getIndex());
  168. }
  169. // This test verifies that the destination address of the response message
  170. // is set correctly when giaddr and ciaddr is zeroed in the received message
  171. // and the new lease is acquired. The lease address is carried in the
  172. // response message in the yiaddr field. In this case destination address
  173. // of the response should be set to yiaddr if server supports direct responses
  174. // to the client which doesn't have an address yet or broadcast if the server
  175. // doesn't support direct responses.
  176. TEST_F(Dhcpv4SrvTest, adjustIfaceDataSelect) {
  177. IfaceMgrTestConfig test_config(true);
  178. IfaceMgr::instance().openSockets4();
  179. // Create instance of the incoming packet.
  180. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
  181. // Clear giaddr to simulate direct packet.
  182. req->setGiaddr(IOAddress("0.0.0.0"));
  183. // Clear client address as it hasn't got any address configured yet.
  184. req->setCiaddr(IOAddress("0.0.0.0"));
  185. // Let's clear the broadcast flag.
  186. req->setFlags(0);
  187. // This is a non-relayed message, so let's clear hops count.
  188. req->setHops(0);
  189. // The query is sent to the broadcast address in the Select state.
  190. req->setLocalAddr(IOAddress("255.255.255.255"));
  191. // The query has been received on the DHCPv4 server port 67.
  192. req->setLocalPort(DHCP4_SERVER_PORT);
  193. // Set the interface. The response should be sent via the same interface.
  194. req->setIface("eth1");
  195. req->setIndex(1);
  196. // Create a response.
  197. boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
  198. // Assign some new address for this client.
  199. resp->setYiaddr(IOAddress("192.0.1.13"));
  200. // Clear the remote address.
  201. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  202. // Copy hops count.
  203. resp->setHops(req->getHops());
  204. // We want to test the case, when the server (packet filter) doesn't support
  205. // ddirect responses to the client which doesn't have an address yet. In
  206. // case, the server should send its response to the broadcast address.
  207. // We can control whether the current packet filter returns that its support
  208. // direct responses or not.
  209. test_config.setDirectResponse(false);
  210. // When running unit tests, the IfaceMgr is using the default Packet
  211. // Filtering class, PktFilterInet. This class does not support direct
  212. // responses to clients without address assigned. When giaddr and ciaddr
  213. // are zero and client has just got new lease, the assigned address is
  214. // carried in yiaddr. In order to send this address to the client,
  215. // server must broadcast its response.
  216. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
  217. // Check that the response is sent to broadcast address as the
  218. // server doesn't have capability to respond directly.
  219. EXPECT_EQ("255.255.255.255", resp->getRemoteAddr().toText());
  220. // Although the query has been sent to the broadcast address, the
  221. // server should select a unicast address on the particular interface
  222. // as a source address for the response.
  223. EXPECT_EQ("192.0.2.3", resp->getLocalAddr().toText());
  224. // The response should be sent from the DHCPv4 server port.
  225. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
  226. // The response should be sent via the same interface through which
  227. // query has been received.
  228. EXPECT_EQ("eth1", resp->getIface());
  229. EXPECT_EQ(1, resp->getIndex());
  230. // We also want to test the case when the server has capability to
  231. // respond directly to the client which is not configured. Server
  232. // makes decision whether it responds directly or broadcast its
  233. // response based on the capability reported by IfaceMgr. We can
  234. // control whether the current packet filter returns that it supports
  235. // direct responses or not.
  236. test_config.setDirectResponse(true);
  237. // Now we expect that the server will send its response to the
  238. // address assigned for the client.
  239. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
  240. EXPECT_EQ("192.0.1.13", resp->getRemoteAddr().toText());
  241. }
  242. // This test verifies that the destination address of the response message
  243. // is set to broadcast address when client set broadcast flag in its
  244. // query. Client sets this flag to indicate that it can't receive direct
  245. // responses from the server when it doesn't have its interface configured.
  246. // Server must respect broadcast flag.
  247. TEST_F(Dhcpv4SrvTest, adjustIfaceDataBroadcast) {
  248. IfaceMgrTestConfig test_config(true);
  249. IfaceMgr::instance().openSockets4();
  250. // Create instance of the incoming packet.
  251. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
  252. // Clear giaddr to simulate direct packet.
  253. req->setGiaddr(IOAddress("0.0.0.0"));
  254. // Clear client address as it hasn't got any address configured yet.
  255. req->setCiaddr(IOAddress("0.0.0.0"));
  256. // The query is sent to the broadcast address in the Select state.
  257. req->setLocalAddr(IOAddress("255.255.255.255"));
  258. // The query has been received on the DHCPv4 server port 67.
  259. req->setLocalPort(DHCP4_SERVER_PORT);
  260. // Set the interface. The response should be sent via the same interface.
  261. req->setIface("eth1");
  262. req->setIndex(1);
  263. // Let's set the broadcast flag.
  264. req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
  265. // Create a response.
  266. boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
  267. // Assign some new address for this client.
  268. resp->setYiaddr(IOAddress("192.0.1.13"));
  269. // Clear the remote address.
  270. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  271. ASSERT_NO_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp));
  272. // Server must repond to broadcast address when client desired that
  273. // by setting the broadcast flag in its request.
  274. EXPECT_EQ("255.255.255.255", resp->getRemoteAddr().toText());
  275. // Although the query has been sent to the broadcast address, the
  276. // server should select a unicast address on the particular interface
  277. // as a source address for the response.
  278. EXPECT_EQ("192.0.2.3", resp->getLocalAddr().toText());
  279. // The response should be sent from the DHCPv4 server port.
  280. EXPECT_EQ(DHCP4_SERVER_PORT, resp->getLocalPort());
  281. // The response should be sent via the same interface through which
  282. // query has been received.
  283. EXPECT_EQ("eth1", resp->getIface());
  284. EXPECT_EQ(1, resp->getIndex());
  285. }
  286. // This test verifies that exception is thrown of the invalid combination
  287. // of giaddr and hops is specified in a client's message.
  288. TEST_F(Dhcpv4SrvTest, adjustIfaceDataInvalid) {
  289. boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
  290. // The hops and giaddr values are used to determine if the client's
  291. // message has been relayed or sent directly. The allowed combinations
  292. // are (giaddr = 0 and hops = 0) or (giaddr != 0 and hops != 0). Any
  293. // other combination is invalid and the adjustIfaceData should throw
  294. // an exception. We will test that exception is indeed thrown.
  295. req->setGiaddr(IOAddress("0.0.0.0"));
  296. req->setHops(1);
  297. // Clear client address as it hasn't got any address configured yet.
  298. req->setCiaddr(IOAddress("0.0.0.0"));
  299. // The query is sent to the broadcast address in the Select state.
  300. req->setLocalAddr(IOAddress("255.255.255.255"));
  301. // The query has been received on the DHCPv4 server port 67.
  302. req->setLocalPort(DHCP4_SERVER_PORT);
  303. // Set the interface. The response should be sent via the same interface.
  304. req->setIface("eth0");
  305. req->setIndex(1);
  306. // Create a response.
  307. boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
  308. // Assign some new address for this client.
  309. resp->setYiaddr(IOAddress("192.0.2.13"));
  310. // Clear the remote address.
  311. resp->setRemoteAddr(IOAddress("0.0.0.0"));
  312. EXPECT_THROW(NakedDhcpv4Srv::adjustIfaceData(req, resp), isc::BadValue);
  313. }
  314. // This test verifies that the server identifier option is appended to
  315. // a specified DHCPv4 message and the server identifier is correct.
  316. TEST_F(Dhcpv4SrvTest, appendServerID) {
  317. Pkt4Ptr response(new Pkt4(DHCPDISCOVER, 1234));
  318. // Set a local address. It is required by the function under test
  319. // to create the Server Identifier option.
  320. response->setLocalAddr(IOAddress("192.0.3.1"));
  321. // Append the Server Identifier.
  322. ASSERT_NO_THROW(NakedDhcpv4Srv::appendServerID(response));
  323. // Make sure that the option has been added.
  324. OptionPtr opt = response->getOption(DHO_DHCP_SERVER_IDENTIFIER);
  325. ASSERT_TRUE(opt);
  326. Option4AddrLstPtr opt_server_id =
  327. boost::dynamic_pointer_cast<Option4AddrLst>(opt);
  328. ASSERT_TRUE(opt_server_id);
  329. // The option is represented as a list of IPv4 addresses but with
  330. // only one address added.
  331. Option4AddrLst::AddressContainer addrs = opt_server_id->getAddresses();
  332. ASSERT_EQ(1, addrs.size());
  333. // This address should match the local address of the packet.
  334. EXPECT_EQ("192.0.3.1", addrs[0].toText());
  335. }
  336. // Sanity check. Verifies that both Dhcpv4Srv and its derived
  337. // class NakedDhcpv4Srv can be instantiated and destroyed.
  338. TEST_F(Dhcpv4SrvTest, basic) {
  339. // Check that the base class can be instantiated
  340. boost::scoped_ptr<Dhcpv4Srv> srv;
  341. ASSERT_NO_THROW(srv.reset(new Dhcpv4Srv(DHCP4_SERVER_PORT + 10000, false,
  342. false)));
  343. srv.reset();
  344. // We have to close open sockets because further in this test we will
  345. // call the Dhcpv4Srv constructor again. This constructor will try to
  346. // set the appropriate packet filter class for IfaceMgr. This requires
  347. // that all sockets are closed.
  348. IfaceMgr::instance().closeSockets();
  349. // Check that the derived class can be instantiated
  350. boost::scoped_ptr<NakedDhcpv4Srv> naked_srv;
  351. ASSERT_NO_THROW(
  352. naked_srv.reset(new NakedDhcpv4Srv(DHCP4_SERVER_PORT + 10000)));
  353. // Close sockets again for the next test.
  354. IfaceMgr::instance().closeSockets();
  355. ASSERT_NO_THROW(naked_srv.reset(new NakedDhcpv4Srv(0)));
  356. }
  357. // This test verifies that exception is not thrown when an error occurs during
  358. // opening sockets. This test forces an error by adding a fictious interface
  359. // to the IfaceMgr. An attempt to open socket on this interface must always
  360. // fail. The DHCPv4 installs the error handler function to prevent exceptions
  361. // being thrown from the openSockets4 function.
  362. // @todo The server tests for socket should be extended but currently the
  363. // ability to unit test the sockets code is somewhat limited.
  364. TEST_F(Dhcpv4SrvTest, openActiveSockets) {
  365. ASSERT_NO_THROW(CfgMgr::instance().activateAllIfaces());
  366. Iface iface("bogusiface", 255);
  367. iface.flag_loopback_ = false;
  368. iface.flag_up_ = true;
  369. iface.flag_running_ = true;
  370. iface.inactive4_ = false;
  371. iface.addAddress(IOAddress("192.0.0.0"));
  372. IfaceMgr::instance().addInterface(iface);
  373. ASSERT_NO_THROW(Dhcpv4Srv::openActiveSockets(DHCP4_SERVER_PORT, false));
  374. }
  375. // Verifies that DISCOVER message can be processed correctly,
  376. // that the OFFER message generated in response is valid and
  377. // contains necessary options.
  378. //
  379. // Note: this test focuses on the packet correctness. There
  380. // are other tests that verify correctness of the allocation
  381. // engine. See DiscoverBasic, DiscoverHint, DiscoverNoClientId
  382. // and DiscoverInvalidHint.
  383. TEST_F(Dhcpv4SrvTest, processDiscover) {
  384. testDiscoverRequest(DHCPDISCOVER);
  385. }
  386. // Verifies that REQUEST message can be processed correctly,
  387. // that the OFFER message generated in response is valid and
  388. // contains necessary options.
  389. //
  390. // Note: this test focuses on the packet correctness. There
  391. // are other tests that verify correctness of the allocation
  392. // engine. See DiscoverBasic, DiscoverHint, DiscoverNoClientId
  393. // and DiscoverInvalidHint.
  394. TEST_F(Dhcpv4SrvTest, processRequest) {
  395. testDiscoverRequest(DHCPREQUEST);
  396. }
  397. TEST_F(Dhcpv4SrvTest, processRelease) {
  398. NakedDhcpv4Srv srv;
  399. Pkt4Ptr pkt(new Pkt4(DHCPRELEASE, 1234));
  400. // Should not throw
  401. EXPECT_NO_THROW(srv.processRelease(pkt));
  402. }
  403. TEST_F(Dhcpv4SrvTest, processDecline) {
  404. NakedDhcpv4Srv srv;
  405. Pkt4Ptr pkt(new Pkt4(DHCPDECLINE, 1234));
  406. // Should not throw
  407. EXPECT_NO_THROW(srv.processDecline(pkt));
  408. }
  409. TEST_F(Dhcpv4SrvTest, processInform) {
  410. NakedDhcpv4Srv srv;
  411. Pkt4Ptr pkt(new Pkt4(DHCPINFORM, 1234));
  412. // Should not throw
  413. EXPECT_NO_THROW(srv.processInform(pkt));
  414. // Should return something
  415. EXPECT_TRUE(srv.processInform(pkt));
  416. // @todo Implement more reasonable tests before starting
  417. // work on processSomething() method.
  418. }
  419. TEST_F(Dhcpv4SrvTest, serverReceivedPacketName) {
  420. // Check all possible packet types
  421. for (int itype = 0; itype < 256; ++itype) {
  422. uint8_t type = itype;
  423. switch (type) {
  424. case DHCPDECLINE:
  425. EXPECT_STREQ("DECLINE", Dhcpv4Srv::serverReceivedPacketName(type));
  426. break;
  427. case DHCPDISCOVER:
  428. EXPECT_STREQ("DISCOVER", Dhcpv4Srv::serverReceivedPacketName(type));
  429. break;
  430. case DHCPINFORM:
  431. EXPECT_STREQ("INFORM", Dhcpv4Srv::serverReceivedPacketName(type));
  432. break;
  433. case DHCPRELEASE:
  434. EXPECT_STREQ("RELEASE", Dhcpv4Srv::serverReceivedPacketName(type));
  435. break;
  436. case DHCPREQUEST:
  437. EXPECT_STREQ("REQUEST", Dhcpv4Srv::serverReceivedPacketName(type));
  438. break;
  439. default:
  440. EXPECT_STREQ("UNKNOWN", Dhcpv4Srv::serverReceivedPacketName(type));
  441. }
  442. }
  443. }
  444. // This test verifies that incoming DISCOVER can be handled properly, that an
  445. // OFFER is generated, that the response has an address and that address
  446. // really belongs to the configured pool.
  447. //
  448. // constructed very simple DISCOVER message with:
  449. // - client-id option
  450. //
  451. // expected returned OFFER message:
  452. // - copy of client-id
  453. // - server-id
  454. // - offered address
  455. TEST_F(Dhcpv4SrvTest, DiscoverBasic) {
  456. IfaceMgrTestConfig test_config(true);
  457. IfaceMgr::instance().openSockets4();
  458. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  459. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  460. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  461. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  462. OptionPtr clientid = generateClientId();
  463. dis->addOption(clientid);
  464. dis->setIface("eth1");
  465. // Pass it to the server and get an offer
  466. Pkt4Ptr offer = srv->processDiscover(dis);
  467. // Check if we get response at all
  468. checkResponse(offer, DHCPOFFER, 1234);
  469. // Check that address was returned from proper range, that its lease
  470. // lifetime is correct, that T1 and T2 are returned properly
  471. checkAddressParams(offer, subnet_);
  472. // Check identifiers
  473. checkServerId(offer, srv->getServerID());
  474. checkClientId(offer, clientid);
  475. }
  476. // This test verifies that incoming DISCOVER can be handled properly, that an
  477. // OFFER is generated, that the response has an address and that address
  478. // really belongs to the configured pool.
  479. //
  480. // constructed very simple DISCOVER message with:
  481. // - client-id option
  482. // - address set to specific value as hint
  483. //
  484. // expected returned OFFER message:
  485. // - copy of client-id
  486. // - server-id
  487. // - offered address
  488. TEST_F(Dhcpv4SrvTest, DiscoverHint) {
  489. IfaceMgrTestConfig test_config(true);
  490. IfaceMgr::instance().openSockets4();
  491. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  492. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  493. IOAddress hint("192.0.2.107");
  494. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  495. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  496. OptionPtr clientid = generateClientId();
  497. dis->addOption(clientid);
  498. dis->setYiaddr(hint);
  499. dis->setIface("eth1");
  500. // Pass it to the server and get an offer
  501. Pkt4Ptr offer = srv->processDiscover(dis);
  502. // Check if we get response at all
  503. checkResponse(offer, DHCPOFFER, 1234);
  504. // Check that address was returned from proper range, that its lease
  505. // lifetime is correct, that T1 and T2 are returned properly
  506. checkAddressParams(offer, subnet_);
  507. EXPECT_EQ(offer->getYiaddr(), hint);
  508. // Check identifiers
  509. checkServerId(offer, srv->getServerID());
  510. checkClientId(offer, clientid);
  511. }
  512. // This test verifies that incoming DISCOVER can be handled properly, that an
  513. // OFFER is generated, that the response has an address and that address
  514. // really belongs to the configured pool.
  515. //
  516. // constructed very simple DISCOVER message with:
  517. // - address set to specific value as hint
  518. //
  519. // expected returned OFFER message:
  520. // - copy of client-id
  521. // - server-id
  522. // - offered address
  523. TEST_F(Dhcpv4SrvTest, DiscoverNoClientId) {
  524. IfaceMgrTestConfig test_config(true);
  525. IfaceMgr::instance().openSockets4();
  526. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  527. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  528. IOAddress hint("192.0.2.107");
  529. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  530. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  531. dis->setYiaddr(hint);
  532. dis->setHWAddr(generateHWAddr(6));
  533. dis->setIface("eth1");
  534. // Pass it to the server and get an offer
  535. Pkt4Ptr offer = srv->processDiscover(dis);
  536. // Check if we get response at all
  537. checkResponse(offer, DHCPOFFER, 1234);
  538. // Check that address was returned from proper range, that its lease
  539. // lifetime is correct, that T1 and T2 are returned properly
  540. checkAddressParams(offer, subnet_);
  541. EXPECT_EQ(offer->getYiaddr(), hint);
  542. // Check identifiers
  543. checkServerId(offer, srv->getServerID());
  544. }
  545. // This test verifies that incoming DISCOVER can be handled properly, that an
  546. // OFFER is generated, that the response has an address and that address
  547. // really belongs to the configured pool.
  548. //
  549. // constructed very simple DISCOVER message with:
  550. // - client-id option
  551. // - address set to specific value as hint, but that hint is invalid
  552. //
  553. // expected returned OFFER message:
  554. // - copy of client-id
  555. // - server-id
  556. // - offered address (!= hint)
  557. TEST_F(Dhcpv4SrvTest, DiscoverInvalidHint) {
  558. IfaceMgrTestConfig test_config(true);
  559. IfaceMgr::instance().openSockets4();
  560. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  561. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  562. IOAddress hint("10.1.2.3");
  563. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  564. dis->setRemoteAddr(IOAddress("192.0.2.107"));
  565. OptionPtr clientid = generateClientId();
  566. dis->addOption(clientid);
  567. dis->setYiaddr(hint);
  568. dis->setIface("eth1");
  569. // Pass it to the server and get an offer
  570. Pkt4Ptr offer = srv->processDiscover(dis);
  571. // Check if we get response at all
  572. checkResponse(offer, DHCPOFFER, 1234);
  573. // Check that address was returned from proper range, that its lease
  574. // lifetime is correct, that T1 and T2 are returned properly
  575. checkAddressParams(offer, subnet_);
  576. EXPECT_NE(offer->getYiaddr(), hint);
  577. // Check identifiers
  578. checkServerId(offer, srv->getServerID());
  579. checkClientId(offer, clientid);
  580. }
  581. /// @todo: Add a test that client sends hint that is in pool, but currently
  582. /// being used by a different client.
  583. // This test checks that the server is offering different addresses to different
  584. // clients in OFFERs. Please note that OFFER is not a guarantee that such
  585. // an address will be assigned. Had the pool was very small and contained only
  586. // 2 addresses, the third client would get the same offer as the first one
  587. // and this is a correct behavior. It is REQUEST that will fail for the third
  588. // client. OFFER is basically saying "if you send me a request, you will
  589. // probably get an address like this" (there are no guarantees).
  590. TEST_F(Dhcpv4SrvTest, ManyDiscovers) {
  591. IfaceMgrTestConfig test_config(true);
  592. IfaceMgr::instance().openSockets4();
  593. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  594. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  595. Pkt4Ptr dis1 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  596. Pkt4Ptr dis2 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 2345));
  597. Pkt4Ptr dis3 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 3456));
  598. dis1->setRemoteAddr(IOAddress("192.0.2.1"));
  599. dis2->setRemoteAddr(IOAddress("192.0.2.2"));
  600. dis3->setRemoteAddr(IOAddress("192.0.2.3"));
  601. // Assign interfaces
  602. dis1->setIface("eth1");
  603. dis2->setIface("eth1");
  604. dis3->setIface("eth1");
  605. // Different client-id sizes
  606. OptionPtr clientid1 = generateClientId(4); // length 4
  607. OptionPtr clientid2 = generateClientId(5); // length 5
  608. OptionPtr clientid3 = generateClientId(6); // length 6
  609. dis1->addOption(clientid1);
  610. dis2->addOption(clientid2);
  611. dis3->addOption(clientid3);
  612. // Pass it to the server and get an offer
  613. Pkt4Ptr offer1 = srv->processDiscover(dis1);
  614. Pkt4Ptr offer2 = srv->processDiscover(dis2);
  615. Pkt4Ptr offer3 = srv->processDiscover(dis3);
  616. // Check if we get response at all
  617. checkResponse(offer1, DHCPOFFER, 1234);
  618. checkResponse(offer2, DHCPOFFER, 2345);
  619. checkResponse(offer3, DHCPOFFER, 3456);
  620. IOAddress addr1 = offer1->getYiaddr();
  621. IOAddress addr2 = offer2->getYiaddr();
  622. IOAddress addr3 = offer3->getYiaddr();
  623. // Check that the assigned address is indeed from the configured pool
  624. checkAddressParams(offer1, subnet_);
  625. checkAddressParams(offer2, subnet_);
  626. checkAddressParams(offer3, subnet_);
  627. // Check server-ids
  628. checkServerId(offer1, srv->getServerID());
  629. checkServerId(offer2, srv->getServerID());
  630. checkServerId(offer3, srv->getServerID());
  631. checkClientId(offer1, clientid1);
  632. checkClientId(offer2, clientid2);
  633. checkClientId(offer3, clientid3);
  634. // Finally check that the addresses offered are different
  635. EXPECT_NE(addr1, addr2);
  636. EXPECT_NE(addr2, addr3);
  637. EXPECT_NE(addr3, addr1);
  638. cout << "Offered address to client1=" << addr1 << endl;
  639. cout << "Offered address to client2=" << addr2 << endl;
  640. cout << "Offered address to client3=" << addr3 << endl;
  641. }
  642. // Checks whether echoing back client-id is controllable, i.e.
  643. // whether the server obeys echo-client-id and sends (or not)
  644. // client-id
  645. TEST_F(Dhcpv4SrvTest, discoverEchoClientId) {
  646. IfaceMgrTestConfig test_config(true);
  647. IfaceMgr::instance().openSockets4();
  648. NakedDhcpv4Srv srv(0);
  649. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  650. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  651. OptionPtr clientid = generateClientId();
  652. dis->addOption(clientid);
  653. dis->setIface("eth1");
  654. // Pass it to the server and get an offer
  655. Pkt4Ptr offer = srv.processDiscover(dis);
  656. // Check if we get response at all
  657. checkResponse(offer, DHCPOFFER, 1234);
  658. checkClientId(offer, clientid);
  659. CfgMgr::instance().echoClientId(false);
  660. offer = srv.processDiscover(dis);
  661. // Check if we get response at all
  662. checkResponse(offer, DHCPOFFER, 1234);
  663. checkClientId(offer, clientid);
  664. }
  665. // This test verifies that incoming REQUEST can be handled properly, that an
  666. // ACK is generated, that the response has an address and that address
  667. // really belongs to the configured pool.
  668. //
  669. // constructed a single REQUEST message with:
  670. // - client-id option
  671. // - hwaddr information
  672. // - requested address (that the client received in DISCOVER/OFFER exchange)
  673. //
  674. // expected returned ACK message:
  675. // - copy of client-id
  676. // - server-id
  677. // - assigned address
  678. //
  679. // Test verifies that the lease is actually in the database.
  680. TEST_F(Dhcpv4SrvTest, RequestBasic) {
  681. IfaceMgrTestConfig test_config(true);
  682. IfaceMgr::instance().openSockets4();
  683. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  684. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  685. IOAddress hint("192.0.2.107");
  686. Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  687. req->setRemoteAddr(IOAddress("192.0.2.1"));
  688. OptionPtr clientid = generateClientId();
  689. req->addOption(clientid);
  690. req->setYiaddr(hint);
  691. req->setIface("eth1");
  692. // Pass it to the server and get an advertise
  693. Pkt4Ptr ack = srv->processRequest(req);
  694. // Check if we get response at all
  695. checkResponse(ack, DHCPACK, 1234);
  696. EXPECT_EQ(hint, ack->getYiaddr());
  697. // Check that address was returned from proper range, that its lease
  698. // lifetime is correct, that T1 and T2 are returned properly
  699. checkAddressParams(ack, subnet_);
  700. // Check identifiers
  701. checkServerId(ack, srv->getServerID());
  702. checkClientId(ack, clientid);
  703. // Check that the lease is really in the database
  704. Lease4Ptr l = checkLease(ack, clientid, req->getHWAddr(), hint);
  705. ASSERT_TRUE(l);
  706. LeaseMgrFactory::instance().deleteLease(l->addr_);
  707. }
  708. // This test verifies that incoming REQUEST can be handled properly, that an
  709. // ACK is generated, that the response has an address and that address
  710. // really belongs to the configured pool.
  711. //
  712. // constructed 3 REQUEST messages with:
  713. // - client-id option (differs between messages)
  714. // - hwaddr information (differs between messages)
  715. //
  716. // expected returned ACK message:
  717. // - copy of client-id
  718. // - server-id
  719. // - assigned address (different for each client)
  720. TEST_F(Dhcpv4SrvTest, ManyRequests) {
  721. IfaceMgrTestConfig test_config(true);
  722. IfaceMgr::instance().openSockets4();
  723. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  724. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  725. const IOAddress req_addr1("192.0.2.105");
  726. const IOAddress req_addr2("192.0.2.101");
  727. const IOAddress req_addr3("192.0.2.109");
  728. const IOAddress relay("192.0.2.1");
  729. Pkt4Ptr req1 = Pkt4Ptr(new Pkt4(DHCPOFFER, 1234));
  730. Pkt4Ptr req2 = Pkt4Ptr(new Pkt4(DHCPOFFER, 2345));
  731. Pkt4Ptr req3 = Pkt4Ptr(new Pkt4(DHCPOFFER, 3456));
  732. req1->setRemoteAddr(relay);
  733. req2->setRemoteAddr(relay);
  734. req3->setRemoteAddr(relay);
  735. // Assign interfaces
  736. req1->setIface("eth1");
  737. req2->setIface("eth1");
  738. req3->setIface("eth1");
  739. req1->setYiaddr(req_addr1);
  740. req2->setYiaddr(req_addr2);
  741. req3->setYiaddr(req_addr3);
  742. req1->setHWAddr(generateHWAddr(6));
  743. req2->setHWAddr(generateHWAddr(7));
  744. req3->setHWAddr(generateHWAddr(8));
  745. // Different client-id sizes
  746. OptionPtr clientid1 = generateClientId(4); // length 4
  747. OptionPtr clientid2 = generateClientId(5); // length 5
  748. OptionPtr clientid3 = generateClientId(6); // length 6
  749. req1->addOption(clientid1);
  750. req2->addOption(clientid2);
  751. req3->addOption(clientid3);
  752. // Pass it to the server and get an advertise
  753. Pkt4Ptr ack1 = srv->processRequest(req1);
  754. Pkt4Ptr ack2 = srv->processRequest(req2);
  755. Pkt4Ptr ack3 = srv->processRequest(req3);
  756. // Check if we get response at all
  757. checkResponse(ack1, DHCPACK, 1234);
  758. checkResponse(ack2, DHCPACK, 2345);
  759. checkResponse(ack3, DHCPACK, 3456);
  760. IOAddress addr1 = ack1->getYiaddr();
  761. IOAddress addr2 = ack2->getYiaddr();
  762. IOAddress addr3 = ack3->getYiaddr();
  763. // Check that every client received the address it requested
  764. EXPECT_EQ(req_addr1, addr1);
  765. EXPECT_EQ(req_addr2, addr2);
  766. EXPECT_EQ(req_addr3, addr3);
  767. // Check that the assigned address is indeed from the configured pool
  768. checkAddressParams(ack1, subnet_);
  769. checkAddressParams(ack2, subnet_);
  770. checkAddressParams(ack3, subnet_);
  771. // Check DUIDs
  772. checkServerId(ack1, srv->getServerID());
  773. checkServerId(ack2, srv->getServerID());
  774. checkServerId(ack3, srv->getServerID());
  775. checkClientId(ack1, clientid1);
  776. checkClientId(ack2, clientid2);
  777. checkClientId(ack3, clientid3);
  778. // Check that leases are in the database
  779. Lease4Ptr l = checkLease(ack1, clientid1, req1->getHWAddr(), addr1);
  780. EXPECT_TRUE(l);
  781. l = checkLease(ack2, clientid2, req2->getHWAddr(), addr2);
  782. l = checkLease(ack3, clientid3, req3->getHWAddr(), addr3);
  783. // Finally check that the addresses offered are different
  784. EXPECT_NE(addr1, addr2);
  785. EXPECT_NE(addr2, addr3);
  786. EXPECT_NE(addr3, addr1);
  787. cout << "Offered address to client1=" << addr1 << endl;
  788. cout << "Offered address to client2=" << addr2 << endl;
  789. cout << "Offered address to client3=" << addr3 << endl;
  790. }
  791. // Checks whether echoing back client-id is controllable
  792. TEST_F(Dhcpv4SrvTest, requestEchoClientId) {
  793. IfaceMgrTestConfig test_config(true);
  794. IfaceMgr::instance().openSockets4();
  795. NakedDhcpv4Srv srv(0);
  796. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  797. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  798. OptionPtr clientid = generateClientId();
  799. dis->addOption(clientid);
  800. dis->setIface("eth1");
  801. // Pass it to the server and get ACK
  802. Pkt4Ptr ack = srv.processRequest(dis);
  803. // Check if we get response at all
  804. checkResponse(ack, DHCPACK, 1234);
  805. checkClientId(ack, clientid);
  806. CfgMgr::instance().echoClientId(false);
  807. ack = srv.processDiscover(dis);
  808. // Check if we get response at all
  809. checkResponse(ack, DHCPOFFER, 1234);
  810. checkClientId(ack, clientid);
  811. }
  812. // This test verifies that incoming (positive) REQUEST/Renewing can be handled properly, that a
  813. // REPLY is generated, that the response has an address and that address
  814. // really belongs to the configured pool and that lease is actually renewed.
  815. //
  816. // expected:
  817. // - returned REPLY message has copy of client-id
  818. // - returned REPLY message has server-id
  819. // - returned REPLY message has IA that includes IAADDR
  820. // - lease is actually renewed in LeaseMgr
  821. TEST_F(Dhcpv4SrvTest, RenewBasic) {
  822. IfaceMgrTestConfig test_config(true);
  823. IfaceMgr::instance().openSockets4();
  824. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  825. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  826. const IOAddress addr("192.0.2.106");
  827. const uint32_t temp_t1 = 50;
  828. const uint32_t temp_t2 = 75;
  829. const uint32_t temp_valid = 100;
  830. const time_t temp_timestamp = time(NULL) - 10;
  831. // Generate client-id also sets client_id_ member
  832. OptionPtr clientid = generateClientId();
  833. // Check that the address we are about to use is indeed in pool
  834. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
  835. // let's create a lease and put it in the LeaseMgr
  836. uint8_t hwaddr2[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  837. Lease4Ptr used(new Lease4(IOAddress("192.0.2.106"), hwaddr2, sizeof(hwaddr2),
  838. &client_id_->getDuid()[0], client_id_->getDuid().size(),
  839. temp_valid, temp_t1, temp_t2, temp_timestamp,
  840. subnet_->getID()));
  841. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  842. // Check that the lease is really in the database
  843. Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
  844. ASSERT_TRUE(l);
  845. // Check that T1, T2, preferred, valid and cltt really set.
  846. // Constructed lease looks as if it was assigned 10 seconds ago
  847. // EXPECT_EQ(l->t1_, temp_t1);
  848. // EXPECT_EQ(l->t2_, temp_t2);
  849. EXPECT_EQ(l->valid_lft_, temp_valid);
  850. EXPECT_EQ(l->cltt_, temp_timestamp);
  851. // Let's create a RENEW
  852. Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  853. req->setRemoteAddr(IOAddress(addr));
  854. req->setYiaddr(addr);
  855. req->setCiaddr(addr); // client's address
  856. req->setIface("eth0");
  857. req->addOption(clientid);
  858. req->addOption(srv->getServerID());
  859. // Pass it to the server and hope for a REPLY
  860. Pkt4Ptr ack = srv->processRequest(req);
  861. // Check if we get response at all
  862. checkResponse(ack, DHCPACK, 1234);
  863. EXPECT_EQ(addr, ack->getYiaddr());
  864. // Check that address was returned from proper range, that its lease
  865. // lifetime is correct, that T1 and T2 are returned properly
  866. checkAddressParams(ack, subnet_);
  867. // Check identifiers
  868. checkServerId(ack, srv->getServerID());
  869. checkClientId(ack, clientid);
  870. // Check that the lease is really in the database
  871. l = checkLease(ack, clientid, req->getHWAddr(), addr);
  872. ASSERT_TRUE(l);
  873. // Check that T1, T2, preferred, valid and cltt were really updated
  874. EXPECT_EQ(l->t1_, subnet_->getT1());
  875. EXPECT_EQ(l->t2_, subnet_->getT2());
  876. EXPECT_EQ(l->valid_lft_, subnet_->getValid());
  877. // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
  878. int32_t cltt = static_cast<int32_t>(l->cltt_);
  879. int32_t expected = static_cast<int32_t>(time(NULL));
  880. // Equality or difference by 1 between cltt and expected is ok.
  881. EXPECT_GE(1, abs(cltt - expected));
  882. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  883. }
  884. // This test verifies that the logic which matches server identifier in the
  885. // received message with server identifiers used by a server works correctly:
  886. // - a message with no server identifier is accepted,
  887. // - a message with a server identifier which matches one of the server
  888. // identifiers used by a server is accepted,
  889. // - a message with a server identifier which doesn't match any server
  890. // identifier used by a server, is not accepted.
  891. TEST_F(Dhcpv4SrvTest, acceptServerId) {
  892. IfaceMgrTestConfig test_config(true);
  893. IfaceMgr::instance().openSockets4();
  894. NakedDhcpv4Srv srv(0);
  895. Pkt4Ptr pkt(new Pkt4(DHCPREQUEST, 1234));
  896. // If no server identifier option is present, the message is always
  897. // accepted.
  898. EXPECT_TRUE(srv.acceptServerId(pkt));
  899. // Create definition of the server identifier option.
  900. OptionDefinition def("server-identifier", DHO_DHCP_SERVER_IDENTIFIER,
  901. "ipv4-address", false);
  902. // Add a server identifier option which doesn't match server ids being
  903. // used by the server. The accepted server ids are the IPv4 addresses
  904. // configured on the interfaces. The 10.1.2.3 is not configured on
  905. // any interfaces.
  906. OptionCustomPtr other_serverid(new OptionCustom(def, Option::V6));
  907. other_serverid->writeAddress(IOAddress("10.1.2.3"));
  908. pkt->addOption(other_serverid);
  909. EXPECT_FALSE(srv.acceptServerId(pkt));
  910. // Remove the server identifier.
  911. ASSERT_NO_THROW(pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER));
  912. // Add a server id being an IPv4 address configured on eth0 interface.
  913. // A DHCPv4 message holding this server identifier should be accepted.
  914. OptionCustomPtr eth0_serverid(new OptionCustom(def, Option::V6));
  915. eth0_serverid->writeAddress(IOAddress("192.0.2.3"));
  916. ASSERT_NO_THROW(pkt->addOption(eth0_serverid));
  917. EXPECT_TRUE(srv.acceptServerId(pkt));
  918. // Remove the server identifier.
  919. ASSERT_NO_THROW(pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER));
  920. // Add a server id being an IPv4 address configured on eth1 interface.
  921. // A DHCPv4 message holding this server identifier should be accepted.
  922. OptionCustomPtr eth1_serverid(new OptionCustom(def, Option::V6));
  923. eth1_serverid->writeAddress(IOAddress("10.0.0.1"));
  924. ASSERT_NO_THROW(pkt->addOption(eth1_serverid));
  925. EXPECT_TRUE(srv.acceptServerId(pkt));
  926. }
  927. // @todo: Implement tests for rejecting renewals
  928. // This test verifies if the sanityCheck() really checks options presence.
  929. TEST_F(Dhcpv4SrvTest, sanityCheck) {
  930. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  931. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  932. Pkt4Ptr pkt = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  933. pkt->setHWAddr(generateHWAddr(6));
  934. // Server-id is optional for information-request, so
  935. EXPECT_NO_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::OPTIONAL));
  936. // Empty packet, no server-id
  937. EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY),
  938. RFCViolation);
  939. pkt->addOption(srv->getServerID());
  940. // Server-id is mandatory and present = no exception
  941. EXPECT_NO_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY));
  942. // Server-id is forbidden, but present => exception
  943. EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::FORBIDDEN),
  944. RFCViolation);
  945. // There's no client-id and no HWADDR. Server needs something to
  946. // identify the client
  947. pkt->setHWAddr(generateHWAddr(0));
  948. EXPECT_THROW(NakedDhcpv4Srv::sanityCheck(pkt, Dhcpv4Srv::MANDATORY),
  949. RFCViolation);
  950. }
  951. // This test verifies that incoming (positive) RELEASE can be handled properly.
  952. // As there is no REPLY in DHCPv4, the only thing to verify here is that
  953. // the lease is indeed removed from the database.
  954. TEST_F(Dhcpv4SrvTest, ReleaseBasic) {
  955. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  956. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  957. const IOAddress addr("192.0.2.106");
  958. const uint32_t temp_t1 = 50;
  959. const uint32_t temp_t2 = 75;
  960. const uint32_t temp_valid = 100;
  961. const time_t temp_timestamp = time(NULL) - 10;
  962. // Generate client-id also duid_
  963. OptionPtr clientid = generateClientId();
  964. // Check that the address we are about to use is indeed in pool
  965. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
  966. // Let's create a lease and put it in the LeaseMgr
  967. uint8_t mac_addr[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  968. HWAddrPtr hw(new HWAddr(mac_addr, sizeof(mac_addr), HTYPE_ETHER));
  969. Lease4Ptr used(new Lease4(addr, mac_addr, sizeof(mac_addr),
  970. &client_id_->getDuid()[0], client_id_->getDuid().size(),
  971. temp_valid, temp_t1, temp_t2, temp_timestamp,
  972. subnet_->getID()));
  973. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  974. // Check that the lease is really in the database
  975. Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
  976. ASSERT_TRUE(l);
  977. // Let's create a RELEASE
  978. // Generate client-id also duid_
  979. Pkt4Ptr rel = Pkt4Ptr(new Pkt4(DHCPRELEASE, 1234));
  980. rel->setRemoteAddr(addr);
  981. rel->setCiaddr(addr);
  982. rel->addOption(clientid);
  983. rel->addOption(srv->getServerID());
  984. rel->setHWAddr(hw);
  985. rel->setIface("eth0");
  986. // Pass it to the server and hope for a REPLY
  987. // Note: this is no response to RELEASE in DHCPv4
  988. EXPECT_NO_THROW(srv->processRelease(rel));
  989. // The lease should be gone from LeaseMgr
  990. l = LeaseMgrFactory::instance().getLease4(addr);
  991. EXPECT_FALSE(l);
  992. // Try to get the lease by hardware address
  993. Lease4Collection leases = LeaseMgrFactory::instance().getLease4(hw->hwaddr_);
  994. EXPECT_EQ(leases.size(), 0);
  995. // Try to get it by hw/subnet_id combination
  996. l = LeaseMgrFactory::instance().getLease4(hw->hwaddr_, subnet_->getID());
  997. EXPECT_FALSE(l);
  998. // Try by client-id
  999. leases = LeaseMgrFactory::instance().getLease4(*client_id_);
  1000. EXPECT_EQ(leases.size(), 0);
  1001. // Try by client-id/subnet-id
  1002. l = LeaseMgrFactory::instance().getLease4(*client_id_, subnet_->getID());
  1003. EXPECT_FALSE(l);
  1004. // Ok, the lease is *really* not there.
  1005. }
  1006. // This test verifies that incoming (invalid) RELEASE can be handled properly.
  1007. //
  1008. // This test checks 3 scenarios:
  1009. // 1. there is no such lease at all
  1010. // 2. there is such a lease, but it is assigned to a different IAID
  1011. // 3. there is such a lease, but it belongs to a different client
  1012. TEST_F(Dhcpv4SrvTest, ReleaseReject) {
  1013. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  1014. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  1015. const IOAddress addr("192.0.2.106");
  1016. const uint32_t t1 = 50;
  1017. const uint32_t t2 = 75;
  1018. const uint32_t valid = 100;
  1019. const time_t timestamp = time(NULL) - 10;
  1020. // Let's create a lease and put it in the LeaseMgr
  1021. uint8_t bogus_mac_addr[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  1022. HWAddrPtr bogus_hw(new HWAddr(bogus_mac_addr, sizeof(bogus_mac_addr), HTYPE_ETHER));
  1023. OptionPtr bogus_clientid = generateClientId(7); // different length
  1024. // Generate client-id also duid_
  1025. OptionPtr clientid = generateClientId();
  1026. // Check that the address we are about to use is indeed in pool
  1027. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
  1028. // Let's create a RELEASE
  1029. // Generate client-id also duid_
  1030. Pkt4Ptr rel = Pkt4Ptr(new Pkt4(DHCPRELEASE, 1234));
  1031. rel->setRemoteAddr(addr);
  1032. rel->setCiaddr(addr);
  1033. rel->addOption(clientid);
  1034. rel->addOption(srv->getServerID());
  1035. rel->setHWAddr(bogus_hw);
  1036. rel->setIface("eth0");
  1037. // Case 1: No lease known to server
  1038. SCOPED_TRACE("CASE 1: Lease is not known to the server");
  1039. // There is nothing to check here. The lease is not there and server does
  1040. // not send anything back. This case is enumerated here just for keeping
  1041. // parity with similar test in DHCPv6.
  1042. EXPECT_NO_THROW(srv->processRelease(rel));
  1043. // CASE 2: Lease is known and belongs to this client, but to a different hardware
  1044. SCOPED_TRACE("CASE 2: Lease is known and belongs to this client, but uses different HW addr");
  1045. // Let's create a lease and put it in the LeaseMgr
  1046. uint8_t mac_addr[] = { 0, 0x1, 0x2, 0x3, 0x4, 0x5};
  1047. HWAddrPtr hw(new HWAddr(mac_addr, sizeof(mac_addr), HTYPE_ETHER));
  1048. Lease4Ptr used(new Lease4(addr, mac_addr, sizeof(mac_addr),
  1049. &client_id_->getDuid()[0], client_id_->getDuid().size(),
  1050. valid, t1, t2, timestamp, subnet_->getID()));
  1051. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  1052. // Check that the lease is really in the database
  1053. Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
  1054. ASSERT_TRUE(l);
  1055. rel->setHWAddr(bogus_hw);
  1056. EXPECT_NO_THROW(srv->processRelease(rel));
  1057. // Check that the lease was not removed (due to hardware address mis-match)
  1058. l = LeaseMgrFactory::instance().getLease4(addr);
  1059. ASSERT_TRUE(l);
  1060. // CASE 3: Lease belongs to a client with different client-id
  1061. SCOPED_TRACE("CASE 3: Lease belongs to a client with different client-id");
  1062. rel->setHWAddr(hw); // proper HW address this time
  1063. rel->delOption(DHO_DHCP_CLIENT_IDENTIFIER);
  1064. rel->addOption(bogus_clientid); // but invalid client-id
  1065. OptionPtr x = rel->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
  1066. EXPECT_NO_THROW(srv->processRelease(rel));
  1067. // Check that the lease is still there
  1068. l = LeaseMgrFactory::instance().getLease4(addr);
  1069. ASSERT_TRUE(l);
  1070. // Final sanity check. Verify that with valid hw and client-id release is successful
  1071. rel->delOption(DHO_DHCP_CLIENT_IDENTIFIER);
  1072. rel->addOption(clientid);
  1073. // It should work this time
  1074. EXPECT_NO_THROW(srv->processRelease(rel));
  1075. // Check that the lease is not there
  1076. l = LeaseMgrFactory::instance().getLease4(addr);
  1077. EXPECT_FALSE(l);
  1078. }
  1079. // Checks if received relay agent info option is echoed back to the client
  1080. TEST_F(Dhcpv4SrvTest, relayAgentInfoEcho) {
  1081. IfaceMgrTestConfig test_config(true);
  1082. IfaceMgr::instance().openSockets4();
  1083. NakedDhcpv4Srv srv(0);
  1084. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  1085. // added option 82 (relay agent info) with 3 suboptions. The server
  1086. // is supposed to echo it back in its response.
  1087. Pkt4Ptr dis;
  1088. ASSERT_NO_THROW(dis = captureRelayedDiscover());
  1089. // Simulate that we have received that traffic
  1090. srv.fakeReceive(dis);
  1091. // Server will now process to run its normal loop, but instead of calling
  1092. // IfaceMgr::receive4(), it will read all packets from the list set by
  1093. // fakeReceive()
  1094. // In particular, it should call registered buffer4_receive callback.
  1095. srv.run();
  1096. // Check that the server did send a reposonse
  1097. ASSERT_EQ(1, srv.fake_sent_.size());
  1098. // Make sure that we received a response
  1099. Pkt4Ptr offer = srv.fake_sent_.front();
  1100. ASSERT_TRUE(offer);
  1101. // Get Relay Agent Info from query...
  1102. OptionPtr rai_query = dis->getOption(DHO_DHCP_AGENT_OPTIONS);
  1103. ASSERT_TRUE(rai_query);
  1104. // Get Relay Agent Info from response...
  1105. OptionPtr rai_response = offer->getOption(DHO_DHCP_AGENT_OPTIONS);
  1106. ASSERT_TRUE(rai_response);
  1107. EXPECT_TRUE(rai_response->equal(rai_query));
  1108. }
  1109. /// @todo move vendor options tests to a separate file.
  1110. /// @todo Add more extensive vendor options tests, including multiple
  1111. /// vendor options
  1112. // Checks if vendor options are parsed correctly and requested vendor options
  1113. // are echoed back.
  1114. TEST_F(Dhcpv4SrvTest, vendorOptionsDocsis) {
  1115. IfaceMgrTestConfig test_config(true);
  1116. IfaceMgr::instance().openSockets4();
  1117. NakedDhcpv4Srv srv(0);
  1118. string config = "{ \"interfaces\": [ \"*\" ],"
  1119. "\"rebind-timer\": 2000, "
  1120. "\"renew-timer\": 1000, "
  1121. " \"option-data\": [ {"
  1122. " \"name\": \"tftp-servers\","
  1123. " \"space\": \"vendor-4491\","
  1124. " \"code\": 2,"
  1125. " \"data\": \"10.253.175.16\","
  1126. " \"csv-format\": True"
  1127. " }],"
  1128. "\"subnet4\": [ { "
  1129. " \"pool\": [ \"10.254.226.0/25\" ],"
  1130. " \"subnet\": \"10.254.226.0/24\", "
  1131. " \"rebind-timer\": 2000, "
  1132. " \"renew-timer\": 1000, "
  1133. " \"valid-lifetime\": 4000,"
  1134. " \"interface\": \"eth0\" "
  1135. " } ],"
  1136. "\"valid-lifetime\": 4000 }";
  1137. ElementPtr json = Element::fromJSON(config);
  1138. ConstElementPtr status;
  1139. // Configure the server and make sure the config is accepted
  1140. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1141. ASSERT_TRUE(status);
  1142. comment_ = config::parseAnswer(rcode_, status);
  1143. ASSERT_EQ(0, rcode_);
  1144. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  1145. // added option 82 (relay agent info) with 3 suboptions. The server
  1146. // is supposed to echo it back in its response.
  1147. Pkt4Ptr dis;
  1148. ASSERT_NO_THROW(dis = captureRelayedDiscover());
  1149. // Simulate that we have received that traffic
  1150. srv.fakeReceive(dis);
  1151. // Server will now process to run its normal loop, but instead of calling
  1152. // IfaceMgr::receive4(), it will read all packets from the list set by
  1153. // fakeReceive()
  1154. // In particular, it should call registered buffer4_receive callback.
  1155. srv.run();
  1156. // Check that the server did send a reposonse
  1157. ASSERT_EQ(1, srv.fake_sent_.size());
  1158. // Make sure that we received a response
  1159. Pkt4Ptr offer = srv.fake_sent_.front();
  1160. ASSERT_TRUE(offer);
  1161. // Get Relay Agent Info from query...
  1162. OptionPtr vendor_opt_response = offer->getOption(DHO_VIVSO_SUBOPTIONS);
  1163. ASSERT_TRUE(vendor_opt_response);
  1164. // Check if it's of a correct type
  1165. boost::shared_ptr<OptionVendor> vendor_opt =
  1166. boost::dynamic_pointer_cast<OptionVendor>(vendor_opt_response);
  1167. ASSERT_TRUE(vendor_opt);
  1168. // Get Relay Agent Info from response...
  1169. OptionPtr tftp_servers_generic = vendor_opt->getOption(DOCSIS3_V4_TFTP_SERVERS);
  1170. ASSERT_TRUE(tftp_servers_generic);
  1171. Option4AddrLstPtr tftp_servers =
  1172. boost::dynamic_pointer_cast<Option4AddrLst>(tftp_servers_generic);
  1173. ASSERT_TRUE(tftp_servers);
  1174. Option4AddrLst::AddressContainer addrs = tftp_servers->getAddresses();
  1175. ASSERT_EQ(1, addrs.size());
  1176. EXPECT_EQ("10.253.175.16", addrs[0].toText());
  1177. }
  1178. /// @todo Implement tests for subnetSelect See tests in dhcp6_srv_unittest.cc:
  1179. /// selectSubnetAddr, selectSubnetIface, selectSubnetRelayLinkaddr,
  1180. /// selectSubnetRelayInterfaceId. Note that the concept of interface-id is not
  1181. /// present in the DHCPv4, so not everything is applicable directly.
  1182. /// See ticket #3057
  1183. // Checks if hooks are registered properly.
  1184. TEST_F(Dhcpv4SrvTest, Hooks) {
  1185. NakedDhcpv4Srv srv(0);
  1186. // check if appropriate hooks are registered
  1187. int hook_index_buffer4_receive = -1;
  1188. int hook_index_pkt4_receive = -1;
  1189. int hook_index_select_subnet = -1;
  1190. int hook_index_lease4_release = -1;
  1191. int hook_index_pkt4_send = -1;
  1192. int hook_index_buffer4_send = -1;
  1193. // check if appropriate indexes are set
  1194. EXPECT_NO_THROW(hook_index_buffer4_receive = ServerHooks::getServerHooks()
  1195. .getIndex("buffer4_receive"));
  1196. EXPECT_NO_THROW(hook_index_pkt4_receive = ServerHooks::getServerHooks()
  1197. .getIndex("pkt4_receive"));
  1198. EXPECT_NO_THROW(hook_index_select_subnet = ServerHooks::getServerHooks()
  1199. .getIndex("subnet4_select"));
  1200. EXPECT_NO_THROW(hook_index_lease4_release = ServerHooks::getServerHooks()
  1201. .getIndex("lease4_release"));
  1202. EXPECT_NO_THROW(hook_index_pkt4_send = ServerHooks::getServerHooks()
  1203. .getIndex("pkt4_send"));
  1204. EXPECT_NO_THROW(hook_index_buffer4_send = ServerHooks::getServerHooks()
  1205. .getIndex("buffer4_send"));
  1206. EXPECT_TRUE(hook_index_buffer4_receive > 0);
  1207. EXPECT_TRUE(hook_index_pkt4_receive > 0);
  1208. EXPECT_TRUE(hook_index_select_subnet > 0);
  1209. EXPECT_TRUE(hook_index_lease4_release > 0);
  1210. EXPECT_TRUE(hook_index_pkt4_send > 0);
  1211. EXPECT_TRUE(hook_index_buffer4_send > 0);
  1212. }
  1213. // This test verifies that the following option structure can be parsed:
  1214. // - option (option space 'foobar')
  1215. // - sub option (option space 'foo')
  1216. // - sub option (option space 'bar')
  1217. // @todo Add more thorough unit tests for unpackOptions.
  1218. TEST_F(Dhcpv4SrvTest, unpackOptions) {
  1219. // Create option definition for each level of encapsulation. Each option
  1220. // definition is for the option code 1. Options may have the same
  1221. // option code because they belong to different option spaces.
  1222. // Top level option encapsulates options which belong to 'space-foo'.
  1223. OptionDefinitionPtr opt_def(new OptionDefinition("option-foobar", 1, "uint32",
  1224. "space-foo"));\
  1225. // Middle option encapsulates options which belong to 'space-bar'
  1226. OptionDefinitionPtr opt_def2(new OptionDefinition("option-foo", 1, "uint16",
  1227. "space-bar"));
  1228. // Low level option doesn't encapsulate any option space.
  1229. OptionDefinitionPtr opt_def3(new OptionDefinition("option-bar", 1,
  1230. "uint8"));
  1231. // Add option definitions to the Configuration Manager. Each goes under
  1232. // different option space.
  1233. CfgMgr& cfgmgr = CfgMgr::instance();
  1234. ASSERT_NO_THROW(cfgmgr.addOptionDef(opt_def, "space-foobar"));
  1235. ASSERT_NO_THROW(cfgmgr.addOptionDef(opt_def2, "space-foo"));
  1236. ASSERT_NO_THROW(cfgmgr.addOptionDef(opt_def3, "space-bar"));
  1237. // Create the buffer holding the structure of options.
  1238. const char raw_data[] = {
  1239. // First option starts here.
  1240. 0x01, // option code = 1
  1241. 0x0B, // option length = 11
  1242. 0x00, 0x01, 0x02, 0x03, // This option carries uint32 value
  1243. // Sub option starts here.
  1244. 0x01, // option code = 1
  1245. 0x05, // option length = 5
  1246. 0x01, 0x02, // this option carries uint16 value
  1247. // Last option starts here.
  1248. 0x01, // option code = 1
  1249. 0x01, // option length = 1
  1250. 0x00 // This option carries a single uint8
  1251. // value and has no sub options.
  1252. };
  1253. OptionBuffer buf(raw_data, raw_data + sizeof(raw_data));
  1254. // Parse options.
  1255. NakedDhcpv4Srv srv(0);
  1256. OptionCollection options;
  1257. ASSERT_NO_THROW(srv.unpackOptions(buf, "space-foobar", options));
  1258. // There should be one top level option.
  1259. ASSERT_EQ(1, options.size());
  1260. boost::shared_ptr<OptionInt<uint32_t> > option_foobar =
  1261. boost::dynamic_pointer_cast<OptionInt<uint32_t> >(options.begin()->
  1262. second);
  1263. ASSERT_TRUE(option_foobar);
  1264. EXPECT_EQ(1, option_foobar->getType());
  1265. EXPECT_EQ(0x00010203, option_foobar->getValue());
  1266. // There should be a middle level option held in option_foobar.
  1267. boost::shared_ptr<OptionInt<uint16_t> > option_foo =
  1268. boost::dynamic_pointer_cast<OptionInt<uint16_t> >(option_foobar->
  1269. getOption(1));
  1270. ASSERT_TRUE(option_foo);
  1271. EXPECT_EQ(1, option_foo->getType());
  1272. EXPECT_EQ(0x0102, option_foo->getValue());
  1273. // Finally, there should be a low level option under option_foo.
  1274. boost::shared_ptr<OptionInt<uint8_t> > option_bar =
  1275. boost::dynamic_pointer_cast<OptionInt<uint8_t> >(option_foo->getOption(1));
  1276. ASSERT_TRUE(option_bar);
  1277. EXPECT_EQ(1, option_bar->getType());
  1278. EXPECT_EQ(0x0, option_bar->getValue());
  1279. }
  1280. // Checks whether the server uses default (0.0.0.0) siaddr value, unless
  1281. // explicitly specified
  1282. TEST_F(Dhcpv4SrvTest, siaddrDefault) {
  1283. IfaceMgrTestConfig test_config(true);
  1284. IfaceMgr::instance().openSockets4();
  1285. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  1286. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  1287. IOAddress hint("192.0.2.107");
  1288. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1289. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1290. OptionPtr clientid = generateClientId();
  1291. dis->addOption(clientid);
  1292. dis->setYiaddr(hint);
  1293. dis->setIface("eth1");
  1294. // Pass it to the server and get an offer
  1295. Pkt4Ptr offer = srv->processDiscover(dis);
  1296. ASSERT_TRUE(offer);
  1297. // Check if we get response at all
  1298. checkResponse(offer, DHCPOFFER, 1234);
  1299. // Verify that it is 0.0.0.0
  1300. EXPECT_EQ("0.0.0.0", offer->getSiaddr().toText());
  1301. }
  1302. // Checks whether the server uses specified siaddr value
  1303. TEST_F(Dhcpv4SrvTest, siaddr) {
  1304. IfaceMgrTestConfig test_config(true);
  1305. IfaceMgr::instance().openSockets4();
  1306. boost::scoped_ptr<NakedDhcpv4Srv> srv;
  1307. ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
  1308. subnet_->setSiaddr(IOAddress("192.0.2.123"));
  1309. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1310. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1311. dis->setIface("eth1");
  1312. OptionPtr clientid = generateClientId();
  1313. dis->addOption(clientid);
  1314. // Pass it to the server and get an offer
  1315. Pkt4Ptr offer = srv->processDiscover(dis);
  1316. ASSERT_TRUE(offer);
  1317. // Check if we get response at all
  1318. checkResponse(offer, DHCPOFFER, 1234);
  1319. // Verify that its value is proper
  1320. EXPECT_EQ("192.0.2.123", offer->getSiaddr().toText());
  1321. }
  1322. // Checks if the next-server defined as global value is overridden by subnet
  1323. // specific value and returned in server messages. There's also similar test for
  1324. // checking parser only configuration, see Dhcp4ParserTest.nextServerOverride in
  1325. // config_parser_unittest.cc.
  1326. TEST_F(Dhcpv4SrvTest, nextServerOverride) {
  1327. IfaceMgrTestConfig test_config(true);
  1328. IfaceMgr::instance().openSockets4();
  1329. NakedDhcpv4Srv srv(0);
  1330. ConstElementPtr status;
  1331. string config = "{ \"interfaces\": [ \"*\" ],"
  1332. "\"rebind-timer\": 2000, "
  1333. "\"renew-timer\": 1000, "
  1334. "\"next-server\": \"192.0.0.1\", "
  1335. "\"subnet4\": [ { "
  1336. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1337. " \"next-server\": \"1.2.3.4\", "
  1338. " \"subnet\": \"192.0.2.0/24\" } ],"
  1339. "\"valid-lifetime\": 4000 }";
  1340. ElementPtr json = Element::fromJSON(config);
  1341. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1342. // check if returned status is OK
  1343. ASSERT_TRUE(status);
  1344. comment_ = config::parseAnswer(rcode_, status);
  1345. ASSERT_EQ(0, rcode_);
  1346. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1347. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1348. dis->setIface("eth1");
  1349. OptionPtr clientid = generateClientId();
  1350. dis->addOption(clientid);
  1351. // Pass it to the server and get an offer
  1352. Pkt4Ptr offer = srv.processDiscover(dis);
  1353. ASSERT_TRUE(offer);
  1354. EXPECT_EQ(DHCPOFFER, offer->getType());
  1355. EXPECT_EQ("1.2.3.4", offer->getSiaddr().toText());
  1356. }
  1357. // Checks if the next-server defined as global value is used in responses
  1358. // when there is no specific value defined in subnet and returned to the client
  1359. // properly. There's also similar test for checking parser only configuration,
  1360. // see Dhcp4ParserTest.nextServerGlobal in config_parser_unittest.cc.
  1361. TEST_F(Dhcpv4SrvTest, nextServerGlobal) {
  1362. IfaceMgrTestConfig test_config(true);
  1363. IfaceMgr::instance().openSockets4();
  1364. NakedDhcpv4Srv srv(0);
  1365. ConstElementPtr status;
  1366. string config = "{ \"interfaces\": [ \"*\" ],"
  1367. "\"rebind-timer\": 2000, "
  1368. "\"renew-timer\": 1000, "
  1369. "\"next-server\": \"192.0.0.1\", "
  1370. "\"subnet4\": [ { "
  1371. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1372. " \"subnet\": \"192.0.2.0/24\" } ],"
  1373. "\"valid-lifetime\": 4000 }";
  1374. ElementPtr json = Element::fromJSON(config);
  1375. EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
  1376. // check if returned status is OK
  1377. ASSERT_TRUE(status);
  1378. comment_ = config::parseAnswer(rcode_, status);
  1379. ASSERT_EQ(0, rcode_);
  1380. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  1381. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  1382. dis->setIface("eth1");
  1383. OptionPtr clientid = generateClientId();
  1384. dis->addOption(clientid);
  1385. // Pass it to the server and get an offer
  1386. Pkt4Ptr offer = srv.processDiscover(dis);
  1387. ASSERT_TRUE(offer);
  1388. EXPECT_EQ(DHCPOFFER, offer->getType());
  1389. EXPECT_EQ("192.0.0.1", offer->getSiaddr().toText());
  1390. }
  1391. // A dummy MAC address, padded with 0s
  1392. const uint8_t dummyChaddr[16] = {0, 1, 2, 3, 4, 5, 0, 0,
  1393. 0, 0, 0, 0, 0, 0, 0, 0 };
  1394. // Let's use some creative test content here (128 chars + \0)
  1395. const uint8_t dummyFile[] = "Lorem ipsum dolor sit amet, consectetur "
  1396. "adipiscing elit. Proin mollis placerat metus, at "
  1397. "lacinia orci ornare vitae. Mauris amet.";
  1398. // Yet another type of test content (64 chars + \0)
  1399. const uint8_t dummySname[] = "Lorem ipsum dolor sit amet, consectetur "
  1400. "adipiscing elit posuere.";
  1401. /// @brief a class dedicated to Hooks testing in DHCPv4 server
  1402. ///
  1403. /// This class has a number of static members, because each non-static
  1404. /// method has implicit 'this' parameter, so it does not match callout
  1405. /// signature and couldn't be registered. Furthermore, static methods
  1406. /// can't modify non-static members (for obvious reasons), so many
  1407. /// fields are declared static. It is still better to keep them as
  1408. /// one class rather than unrelated collection of global objects.
  1409. class HooksDhcpv4SrvTest : public Dhcpv4SrvTest {
  1410. public:
  1411. /// @brief creates Dhcpv4Srv and prepares buffers for callouts
  1412. HooksDhcpv4SrvTest() {
  1413. // Allocate new DHCPv6 Server
  1414. srv_ = new NakedDhcpv4Srv(0);
  1415. // clear static buffers
  1416. resetCalloutBuffers();
  1417. }
  1418. /// @brief destructor (deletes Dhcpv4Srv)
  1419. virtual ~HooksDhcpv4SrvTest() {
  1420. HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("buffer4_receive");
  1421. HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("buffer4_send");
  1422. HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("pkt4_receive");
  1423. HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("pkt4_send");
  1424. HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("subnet4_select");
  1425. HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("lease4_renew");
  1426. HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("lease4_release");
  1427. delete srv_;
  1428. }
  1429. /// @brief creates an option with specified option code
  1430. ///
  1431. /// This method is static, because it is used from callouts
  1432. /// that do not have a pointer to HooksDhcpv4SSrvTest object
  1433. ///
  1434. /// @param option_code code of option to be created
  1435. ///
  1436. /// @return pointer to create option object
  1437. static OptionPtr createOption(uint16_t option_code) {
  1438. char payload[] = {
  1439. 0xa, 0xb, 0xc, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14
  1440. };
  1441. OptionBuffer tmp(payload, payload + sizeof(payload));
  1442. return OptionPtr(new Option(Option::V4, option_code, tmp));
  1443. }
  1444. /// @brief Generates test packet.
  1445. ///
  1446. /// Allocates and generates on-wire buffer that represents test packet, with all
  1447. /// fixed fields set to non-zero values. Content is not always reasonable.
  1448. ///
  1449. /// See generateTestPacket1() function that returns exactly the same packet as
  1450. /// Pkt4 object.
  1451. ///
  1452. /// @return pointer to allocated Pkt4 object
  1453. // Returns a vector containing a DHCPv4 packet header.
  1454. Pkt4Ptr
  1455. generateSimpleDiscover() {
  1456. // That is only part of the header. It contains all "short" fields,
  1457. // larger fields are constructed separately.
  1458. uint8_t hdr[] = {
  1459. 1, 6, 6, 13, // op, htype, hlen, hops,
  1460. 0x12, 0x34, 0x56, 0x78, // transaction-id
  1461. 0, 42, 0x80, 0x00, // 42 secs, BROADCAST flags
  1462. 192, 0, 2, 1, // ciaddr
  1463. 1, 2, 3, 4, // yiaddr
  1464. 192, 0, 2, 255, // siaddr
  1465. 255, 255, 255, 255, // giaddr
  1466. };
  1467. // Initialize the vector with the header fields defined above.
  1468. vector<uint8_t> buf(hdr, hdr + sizeof(hdr));
  1469. // Append the large header fields.
  1470. copy(dummyChaddr, dummyChaddr + Pkt4::MAX_CHADDR_LEN, back_inserter(buf));
  1471. copy(dummySname, dummySname + Pkt4::MAX_SNAME_LEN, back_inserter(buf));
  1472. copy(dummyFile, dummyFile + Pkt4::MAX_FILE_LEN, back_inserter(buf));
  1473. // Should now have all the header, so check. The "static_cast" is used
  1474. // to get round an odd bug whereby the linker appears not to find the
  1475. // definition of DHCPV4_PKT_HDR_LEN if it appears within an EXPECT_EQ().
  1476. EXPECT_EQ(static_cast<size_t>(Pkt4::DHCPV4_PKT_HDR_LEN), buf.size());
  1477. // Add magic cookie
  1478. buf.push_back(0x63);
  1479. buf.push_back(0x82);
  1480. buf.push_back(0x53);
  1481. buf.push_back(0x63);
  1482. // Add message type DISCOVER
  1483. buf.push_back(static_cast<uint8_t>(DHO_DHCP_MESSAGE_TYPE));
  1484. buf.push_back(1); // length (just one byte)
  1485. buf.push_back(static_cast<uint8_t>(DHCPDISCOVER));
  1486. Pkt4Ptr dis(new Pkt4(&buf[0], buf.size()));
  1487. // Interface must be selected for a Discover. Server will use the interface
  1488. // name to select a subnet for a client. This test is using fake interfaces
  1489. // and the fake eth0 interface has IPv4 address matching the subnet
  1490. // currently configured for this test.
  1491. dis->setIface("eth1");
  1492. return (dis);
  1493. }
  1494. /// Test callback that stores received callout name and pkt4 value
  1495. /// @param callout_handle handle passed by the hooks framework
  1496. /// @return always 0
  1497. static int
  1498. buffer4_receive_callout(CalloutHandle& callout_handle) {
  1499. callback_name_ = string("buffer4_receive");
  1500. callout_handle.getArgument("query4", callback_pkt4_);
  1501. callback_argument_names_ = callout_handle.getArgumentNames();
  1502. return (0);
  1503. }
  1504. /// Test callback that changes hwaddr value
  1505. /// @param callout_handle handle passed by the hooks framework
  1506. /// @return always 0
  1507. static int
  1508. buffer4_receive_change_hwaddr(CalloutHandle& callout_handle) {
  1509. Pkt4Ptr pkt;
  1510. callout_handle.getArgument("query4", pkt);
  1511. // If there is at least one option with data
  1512. if (pkt->data_.size() >= Pkt4::DHCPV4_PKT_HDR_LEN) {
  1513. // Offset of the first byte of the CHWADDR field. Let's the first
  1514. // byte to some new value that we could later check
  1515. pkt->data_[28] = 0xff;
  1516. }
  1517. // Carry on as usual
  1518. return buffer4_receive_callout(callout_handle);
  1519. }
  1520. /// Test callback that deletes MAC address
  1521. /// @param callout_handle handle passed by the hooks framework
  1522. /// @return always 0
  1523. static int
  1524. buffer4_receive_delete_hwaddr(CalloutHandle& callout_handle) {
  1525. Pkt4Ptr pkt;
  1526. callout_handle.getArgument("query4", pkt);
  1527. pkt->data_[2] = 0; // offset 2 is hlen, let's set it to zero
  1528. memset(&pkt->data_[28], 0, Pkt4::MAX_CHADDR_LEN); // Clear CHADDR content
  1529. // carry on as usual
  1530. return buffer4_receive_callout(callout_handle);
  1531. }
  1532. /// Test callback that sets skip flag
  1533. /// @param callout_handle handle passed by the hooks framework
  1534. /// @return always 0
  1535. static int
  1536. buffer4_receive_skip(CalloutHandle& callout_handle) {
  1537. callout_handle.setSkip(true);
  1538. // Carry on as usual
  1539. return buffer4_receive_callout(callout_handle);
  1540. }
  1541. /// test callback that stores received callout name and pkt4 value
  1542. /// @param callout_handle handle passed by the hooks framework
  1543. /// @return always 0
  1544. static int
  1545. pkt4_receive_callout(CalloutHandle& callout_handle) {
  1546. callback_name_ = string("pkt4_receive");
  1547. callout_handle.getArgument("query4", callback_pkt4_);
  1548. callback_argument_names_ = callout_handle.getArgumentNames();
  1549. return (0);
  1550. }
  1551. /// test callback that changes client-id value
  1552. /// @param callout_handle handle passed by the hooks framework
  1553. /// @return always 0
  1554. static int
  1555. pkt4_receive_change_clientid(CalloutHandle& callout_handle) {
  1556. Pkt4Ptr pkt;
  1557. callout_handle.getArgument("query4", pkt);
  1558. // get rid of the old client-id
  1559. pkt->delOption(DHO_DHCP_CLIENT_IDENTIFIER);
  1560. // add a new option
  1561. pkt->addOption(createOption(DHO_DHCP_CLIENT_IDENTIFIER));
  1562. // carry on as usual
  1563. return pkt4_receive_callout(callout_handle);
  1564. }
  1565. /// test callback that deletes client-id
  1566. /// @param callout_handle handle passed by the hooks framework
  1567. /// @return always 0
  1568. static int
  1569. pkt4_receive_delete_clientid(CalloutHandle& callout_handle) {
  1570. Pkt4Ptr pkt;
  1571. callout_handle.getArgument("query4", pkt);
  1572. // get rid of the old client-id (and no HWADDR)
  1573. vector<uint8_t> mac;
  1574. pkt->delOption(DHO_DHCP_CLIENT_IDENTIFIER);
  1575. pkt->setHWAddr(1, 0, mac); // HWtype 1, hwardware len = 0
  1576. // carry on as usual
  1577. return pkt4_receive_callout(callout_handle);
  1578. }
  1579. /// test callback that sets skip flag
  1580. /// @param callout_handle handle passed by the hooks framework
  1581. /// @return always 0
  1582. static int
  1583. pkt4_receive_skip(CalloutHandle& callout_handle) {
  1584. Pkt4Ptr pkt;
  1585. callout_handle.getArgument("query4", pkt);
  1586. callout_handle.setSkip(true);
  1587. // carry on as usual
  1588. return pkt4_receive_callout(callout_handle);
  1589. }
  1590. /// Test callback that stores received callout name and pkt4 value
  1591. /// @param callout_handle handle passed by the hooks framework
  1592. /// @return always 0
  1593. static int
  1594. pkt4_send_callout(CalloutHandle& callout_handle) {
  1595. callback_name_ = string("pkt4_send");
  1596. callout_handle.getArgument("response4", callback_pkt4_);
  1597. callback_argument_names_ = callout_handle.getArgumentNames();
  1598. return (0);
  1599. }
  1600. // Test callback that changes server-id
  1601. /// @param callout_handle handle passed by the hooks framework
  1602. /// @return always 0
  1603. static int
  1604. pkt4_send_change_serverid(CalloutHandle& callout_handle) {
  1605. Pkt4Ptr pkt;
  1606. callout_handle.getArgument("response4", pkt);
  1607. // get rid of the old server-id
  1608. pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER);
  1609. // add a new option
  1610. pkt->addOption(createOption(DHO_DHCP_SERVER_IDENTIFIER));
  1611. // carry on as usual
  1612. return pkt4_send_callout(callout_handle);
  1613. }
  1614. /// test callback that deletes server-id
  1615. /// @param callout_handle handle passed by the hooks framework
  1616. /// @return always 0
  1617. static int
  1618. pkt4_send_delete_serverid(CalloutHandle& callout_handle) {
  1619. Pkt4Ptr pkt;
  1620. callout_handle.getArgument("response4", pkt);
  1621. // get rid of the old client-id
  1622. pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER);
  1623. // carry on as usual
  1624. return pkt4_send_callout(callout_handle);
  1625. }
  1626. /// Test callback that sets skip flag
  1627. /// @param callout_handle handle passed by the hooks framework
  1628. /// @return always 0
  1629. static int
  1630. pkt4_send_skip(CalloutHandle& callout_handle) {
  1631. Pkt4Ptr pkt;
  1632. callout_handle.getArgument("response4", pkt);
  1633. callout_handle.setSkip(true);
  1634. // carry on as usual
  1635. return pkt4_send_callout(callout_handle);
  1636. }
  1637. /// Test callback that stores received callout name and pkt4 value
  1638. /// @param callout_handle handle passed by the hooks framework
  1639. /// @return always 0
  1640. static int
  1641. buffer4_send_callout(CalloutHandle& callout_handle) {
  1642. callback_name_ = string("buffer4_send");
  1643. callout_handle.getArgument("response4", callback_pkt4_);
  1644. callback_argument_names_ = callout_handle.getArgumentNames();
  1645. return (0);
  1646. }
  1647. /// Test callback changes the output buffer to a hardcoded value
  1648. /// @param callout_handle handle passed by the hooks framework
  1649. /// @return always 0
  1650. static int
  1651. buffer4_send_change_callout(CalloutHandle& callout_handle) {
  1652. Pkt4Ptr pkt;
  1653. callout_handle.getArgument("response4", pkt);
  1654. // modify buffer to set a diffferent payload
  1655. pkt->getBuffer().clear();
  1656. pkt->getBuffer().writeData(dummyFile, sizeof(dummyFile));
  1657. return (0);
  1658. }
  1659. /// Test callback that stores received callout name and pkt4 value
  1660. /// @param callout_handle handle passed by the hooks framework
  1661. /// @return always 0
  1662. static int
  1663. skip_callout(CalloutHandle& callout_handle) {
  1664. callout_handle.setSkip(true);
  1665. return (0);
  1666. }
  1667. /// Test callback that stores received callout name and subnet4 values
  1668. /// @param callout_handle handle passed by the hooks framework
  1669. /// @return always 0
  1670. static int
  1671. subnet4_select_callout(CalloutHandle& callout_handle) {
  1672. callback_name_ = string("subnet4_select");
  1673. callout_handle.getArgument("query4", callback_pkt4_);
  1674. callout_handle.getArgument("subnet4", callback_subnet4_);
  1675. callout_handle.getArgument("subnet4collection", callback_subnet4collection_);
  1676. callback_argument_names_ = callout_handle.getArgumentNames();
  1677. return (0);
  1678. }
  1679. /// Test callback that picks the other subnet if possible.
  1680. /// @param callout_handle handle passed by the hooks framework
  1681. /// @return always 0
  1682. static int
  1683. subnet4_select_different_subnet_callout(CalloutHandle& callout_handle) {
  1684. // Call the basic calllout to record all passed values
  1685. subnet4_select_callout(callout_handle);
  1686. const Subnet4Collection* subnets;
  1687. Subnet4Ptr subnet;
  1688. callout_handle.getArgument("subnet4", subnet);
  1689. callout_handle.getArgument("subnet4collection", subnets);
  1690. // Let's change to a different subnet
  1691. if (subnets->size() > 1) {
  1692. subnet = (*subnets)[1]; // Let's pick the other subnet
  1693. callout_handle.setArgument("subnet4", subnet);
  1694. }
  1695. return (0);
  1696. }
  1697. /// Test callback that stores received callout name passed parameters
  1698. /// @param callout_handle handle passed by the hooks framework
  1699. /// @return always 0
  1700. static int
  1701. lease4_release_callout(CalloutHandle& callout_handle) {
  1702. callback_name_ = string("lease4_release");
  1703. callout_handle.getArgument("query4", callback_pkt4_);
  1704. callout_handle.getArgument("lease4", callback_lease4_);
  1705. callback_argument_names_ = callout_handle.getArgumentNames();
  1706. return (0);
  1707. }
  1708. /// Test callback that stores received callout name and subnet4 values
  1709. /// @param callout_handle handle passed by the hooks framework
  1710. /// @return always 0
  1711. static int
  1712. lease4_renew_callout(CalloutHandle& callout_handle) {
  1713. callback_name_ = string("lease4_renew");
  1714. callout_handle.getArgument("subnet4", callback_subnet4_);
  1715. callout_handle.getArgument("lease4", callback_lease4_);
  1716. callout_handle.getArgument("hwaddr", callback_hwaddr_);
  1717. callout_handle.getArgument("clientid", callback_clientid_);
  1718. callback_argument_names_ = callout_handle.getArgumentNames();
  1719. return (0);
  1720. }
  1721. /// resets buffers used to store data received by callouts
  1722. void resetCalloutBuffers() {
  1723. callback_name_ = string("");
  1724. callback_pkt4_.reset();
  1725. callback_lease4_.reset();
  1726. callback_hwaddr_.reset();
  1727. callback_clientid_.reset();
  1728. callback_subnet4_.reset();
  1729. callback_subnet4collection_ = NULL;
  1730. callback_argument_names_.clear();
  1731. }
  1732. /// pointer to Dhcpv4Srv that is used in tests
  1733. NakedDhcpv4Srv* srv_;
  1734. // The following fields are used in testing pkt4_receive_callout
  1735. /// String name of the received callout
  1736. static string callback_name_;
  1737. /// Pkt4 structure returned in the callout
  1738. static Pkt4Ptr callback_pkt4_;
  1739. /// Lease4 structure returned in the callout
  1740. static Lease4Ptr callback_lease4_;
  1741. /// Hardware address returned in the callout
  1742. static HWAddrPtr callback_hwaddr_;
  1743. /// Client-id returned in the callout
  1744. static ClientIdPtr callback_clientid_;
  1745. /// Pointer to a subnet received by callout
  1746. static Subnet4Ptr callback_subnet4_;
  1747. /// A list of all available subnets (received by callout)
  1748. static const Subnet4Collection* callback_subnet4collection_;
  1749. /// A list of all received arguments
  1750. static vector<string> callback_argument_names_;
  1751. };
  1752. // The following fields are used in testing pkt4_receive_callout.
  1753. // See fields description in the class for details
  1754. string HooksDhcpv4SrvTest::callback_name_;
  1755. Pkt4Ptr HooksDhcpv4SrvTest::callback_pkt4_;
  1756. Subnet4Ptr HooksDhcpv4SrvTest::callback_subnet4_;
  1757. HWAddrPtr HooksDhcpv4SrvTest::callback_hwaddr_;
  1758. ClientIdPtr HooksDhcpv4SrvTest::callback_clientid_;
  1759. Lease4Ptr HooksDhcpv4SrvTest::callback_lease4_;
  1760. const Subnet4Collection* HooksDhcpv4SrvTest::callback_subnet4collection_;
  1761. vector<string> HooksDhcpv4SrvTest::callback_argument_names_;
  1762. // Checks if callouts installed on pkt4_receive are indeed called and the
  1763. // all necessary parameters are passed.
  1764. //
  1765. // Note that the test name does not follow test naming convention,
  1766. // but the proper hook name is "buffer4_receive".
  1767. TEST_F(HooksDhcpv4SrvTest, Buffer4ReceiveSimple) {
  1768. IfaceMgrTestConfig test_config(true);
  1769. IfaceMgr::instance().openSockets4();
  1770. // Install pkt4_receive_callout
  1771. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1772. "buffer4_receive", buffer4_receive_callout));
  1773. // Let's create a simple DISCOVER
  1774. Pkt4Ptr dis = generateSimpleDiscover();
  1775. // Simulate that we have received that traffic
  1776. srv_->fakeReceive(dis);
  1777. // Server will now process to run its normal loop, but instead of calling
  1778. // IfaceMgr::receive4(), it will read all packets from the list set by
  1779. // fakeReceive()
  1780. // In particular, it should call registered buffer4_receive callback.
  1781. srv_->run();
  1782. // Check that the callback called is indeed the one we installed
  1783. EXPECT_EQ("buffer4_receive", callback_name_);
  1784. // Check that pkt4 argument passing was successful and returned proper value
  1785. EXPECT_TRUE(callback_pkt4_.get() == dis.get());
  1786. // Check that all expected parameters are there
  1787. vector<string> expected_argument_names;
  1788. expected_argument_names.push_back(string("query4"));
  1789. EXPECT_TRUE(expected_argument_names == callback_argument_names_);
  1790. }
  1791. // Checks if callouts installed on buffer4_receive is able to change
  1792. // the values and the parameters are indeed used by the server.
  1793. TEST_F(HooksDhcpv4SrvTest, buffer4ReceiveValueChange) {
  1794. IfaceMgrTestConfig test_config(true);
  1795. IfaceMgr::instance().openSockets4();
  1796. // Install callback that modifies MAC addr of incoming packet
  1797. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1798. "buffer4_receive", buffer4_receive_change_hwaddr));
  1799. // Let's create a simple DISCOVER
  1800. Pkt4Ptr discover = generateSimpleDiscover();
  1801. // Simulate that we have received that traffic
  1802. srv_->fakeReceive(discover);
  1803. // Server will now process to run its normal loop, but instead of calling
  1804. // IfaceMgr::receive6(), it will read all packets from the list set by
  1805. // fakeReceive()
  1806. // In particular, it should call registered buffer4_receive callback.
  1807. srv_->run();
  1808. // Check that the server did send a reposonse
  1809. ASSERT_EQ(1, srv_->fake_sent_.size());
  1810. // Make sure that we received a response
  1811. Pkt4Ptr offer = srv_->fake_sent_.front();
  1812. ASSERT_TRUE(offer);
  1813. // Get client-id...
  1814. HWAddrPtr hwaddr = offer->getHWAddr();
  1815. ASSERT_TRUE(hwaddr); // basic sanity check. HWAddr is always present
  1816. // ... and check if it is the modified value
  1817. ASSERT_FALSE(hwaddr->hwaddr_.empty()); // there must be a MAC address
  1818. EXPECT_EQ(0xff, hwaddr->hwaddr_[0]); // check that its first byte was modified
  1819. }
  1820. // Checks if callouts installed on buffer4_receive is able to set skip flag that
  1821. // will cause the server to not parse the packet. Even though the packet is valid,
  1822. // the server should eventually drop it, because there won't be mandatory options
  1823. // (or rather option objects) in it.
  1824. TEST_F(HooksDhcpv4SrvTest, buffer4ReceiveSkip) {
  1825. IfaceMgrTestConfig test_config(true);
  1826. IfaceMgr::instance().openSockets4();
  1827. // Install pkt4_receive_callout
  1828. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1829. "buffer4_receive", buffer4_receive_skip));
  1830. // Let's create a simple DISCOVER
  1831. Pkt4Ptr discover = generateSimpleDiscover();
  1832. // Simulate that we have received that traffic
  1833. srv_->fakeReceive(discover);
  1834. // Server will now process to run its normal loop, but instead of calling
  1835. // IfaceMgr::receive6(), it will read all packets from the list set by
  1836. // fakeReceive()
  1837. // In particular, it should call registered pkt4_receive callback.
  1838. srv_->run();
  1839. // Check that the server dropped the packet and did not produce any response
  1840. ASSERT_EQ(0, srv_->fake_sent_.size());
  1841. }
  1842. // Checks if callouts installed on pkt4_receive are indeed called and the
  1843. // all necessary parameters are passed.
  1844. //
  1845. // Note that the test name does not follow test naming convention,
  1846. // but the proper hook name is "pkt4_receive".
  1847. TEST_F(HooksDhcpv4SrvTest, pkt4ReceiveSimple) {
  1848. IfaceMgrTestConfig test_config(true);
  1849. IfaceMgr::instance().openSockets4();
  1850. // Install pkt4_receive_callout
  1851. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1852. "pkt4_receive", pkt4_receive_callout));
  1853. // Let's create a simple DISCOVER
  1854. Pkt4Ptr sol = generateSimpleDiscover();
  1855. // Simulate that we have received that traffic
  1856. srv_->fakeReceive(sol);
  1857. // Server will now process to run its normal loop, but instead of calling
  1858. // IfaceMgr::receive4(), it will read all packets from the list set by
  1859. // fakeReceive()
  1860. // In particular, it should call registered pkt4_receive callback.
  1861. srv_->run();
  1862. // check that the callback called is indeed the one we installed
  1863. EXPECT_EQ("pkt4_receive", callback_name_);
  1864. // check that pkt4 argument passing was successful and returned proper value
  1865. EXPECT_TRUE(callback_pkt4_.get() == sol.get());
  1866. // Check that all expected parameters are there
  1867. vector<string> expected_argument_names;
  1868. expected_argument_names.push_back(string("query4"));
  1869. EXPECT_TRUE(expected_argument_names == callback_argument_names_);
  1870. }
  1871. // Checks if callouts installed on pkt4_received is able to change
  1872. // the values and the parameters are indeed used by the server.
  1873. TEST_F(HooksDhcpv4SrvTest, valueChange_pkt4_receive) {
  1874. IfaceMgrTestConfig test_config(true);
  1875. IfaceMgr::instance().openSockets4();
  1876. // Install pkt4_receive_callout
  1877. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1878. "pkt4_receive", pkt4_receive_change_clientid));
  1879. // Let's create a simple DISCOVER
  1880. Pkt4Ptr sol = generateSimpleDiscover();
  1881. // Simulate that we have received that traffic
  1882. srv_->fakeReceive(sol);
  1883. // Server will now process to run its normal loop, but instead of calling
  1884. // IfaceMgr::receive4(), it will read all packets from the list set by
  1885. // fakeReceive()
  1886. // In particular, it should call registered pkt4_receive callback.
  1887. srv_->run();
  1888. // check that the server did send a reposonce
  1889. ASSERT_EQ(1, srv_->fake_sent_.size());
  1890. // Make sure that we received a response
  1891. Pkt4Ptr adv = srv_->fake_sent_.front();
  1892. ASSERT_TRUE(adv);
  1893. // Get client-id...
  1894. OptionPtr clientid = adv->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
  1895. // ... and check if it is the modified value
  1896. OptionPtr expected = createOption(DHO_DHCP_CLIENT_IDENTIFIER);
  1897. EXPECT_TRUE(clientid->equal(expected));
  1898. }
  1899. // Checks if callouts installed on pkt4_received is able to delete
  1900. // existing options and that change impacts server processing (mandatory
  1901. // client-id option is deleted, so the packet is expected to be dropped)
  1902. TEST_F(HooksDhcpv4SrvTest, pkt4ReceiveDeleteClientId) {
  1903. IfaceMgrTestConfig test_config(true);
  1904. IfaceMgr::instance().openSockets4();
  1905. // Install pkt4_receive_callout
  1906. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1907. "pkt4_receive", pkt4_receive_delete_clientid));
  1908. // Let's create a simple DISCOVER
  1909. Pkt4Ptr sol = generateSimpleDiscover();
  1910. // Simulate that we have received that traffic
  1911. srv_->fakeReceive(sol);
  1912. // Server will now process to run its normal loop, but instead of calling
  1913. // IfaceMgr::receive4(), it will read all packets from the list set by
  1914. // fakeReceive()
  1915. // In particular, it should call registered pkt4_receive callback.
  1916. srv_->run();
  1917. // Check that the server dropped the packet and did not send a response
  1918. ASSERT_EQ(0, srv_->fake_sent_.size());
  1919. }
  1920. // Checks if callouts installed on pkt4_received is able to set skip flag that
  1921. // will cause the server to not process the packet (drop), even though it is valid.
  1922. TEST_F(HooksDhcpv4SrvTest, pkt4ReceiveSkip) {
  1923. IfaceMgrTestConfig test_config(true);
  1924. IfaceMgr::instance().openSockets4();
  1925. // Install pkt4_receive_callout
  1926. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1927. "pkt4_receive", pkt4_receive_skip));
  1928. // Let's create a simple DISCOVER
  1929. Pkt4Ptr sol = generateSimpleDiscover();
  1930. // Simulate that we have received that traffic
  1931. srv_->fakeReceive(sol);
  1932. // Server will now process to run its normal loop, but instead of calling
  1933. // IfaceMgr::receive4(), it will read all packets from the list set by
  1934. // fakeReceive()
  1935. // In particular, it should call registered pkt4_receive callback.
  1936. srv_->run();
  1937. // check that the server dropped the packet and did not produce any response
  1938. ASSERT_EQ(0, srv_->fake_sent_.size());
  1939. }
  1940. // Checks if callouts installed on pkt4_send are indeed called and the
  1941. // all necessary parameters are passed.
  1942. TEST_F(HooksDhcpv4SrvTest, pkt4SendSimple) {
  1943. IfaceMgrTestConfig test_config(true);
  1944. IfaceMgr::instance().openSockets4();
  1945. // Install pkt4_receive_callout
  1946. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1947. "pkt4_send", pkt4_send_callout));
  1948. // Let's create a simple DISCOVER
  1949. Pkt4Ptr sol = generateSimpleDiscover();
  1950. // Simulate that we have received that traffic
  1951. srv_->fakeReceive(sol);
  1952. // Server will now process to run its normal loop, but instead of calling
  1953. // IfaceMgr::receive4(), it will read all packets from the list set by
  1954. // fakeReceive()
  1955. // In particular, it should call registered pkt4_receive callback.
  1956. srv_->run();
  1957. // Check that the callback called is indeed the one we installed
  1958. EXPECT_EQ("pkt4_send", callback_name_);
  1959. // Check that there is one packet sent
  1960. ASSERT_EQ(1, srv_->fake_sent_.size());
  1961. Pkt4Ptr adv = srv_->fake_sent_.front();
  1962. // Check that pkt4 argument passing was successful and returned proper value
  1963. EXPECT_TRUE(callback_pkt4_.get() == adv.get());
  1964. // Check that all expected parameters are there
  1965. vector<string> expected_argument_names;
  1966. expected_argument_names.push_back(string("response4"));
  1967. EXPECT_TRUE(expected_argument_names == callback_argument_names_);
  1968. }
  1969. // Checks if callouts installed on pkt4_send is able to change
  1970. // the values and the packet sent contains those changes
  1971. TEST_F(HooksDhcpv4SrvTest, pkt4SendValueChange) {
  1972. IfaceMgrTestConfig test_config(true);
  1973. IfaceMgr::instance().openSockets4();
  1974. // Install pkt4_receive_callout
  1975. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1976. "pkt4_send", pkt4_send_change_serverid));
  1977. // Let's create a simple DISCOVER
  1978. Pkt4Ptr sol = generateSimpleDiscover();
  1979. // Simulate that we have received that traffic
  1980. srv_->fakeReceive(sol);
  1981. // Server will now process to run its normal loop, but instead of calling
  1982. // IfaceMgr::receive4(), it will read all packets from the list set by
  1983. // fakeReceive()
  1984. // In particular, it should call registered pkt4_receive callback.
  1985. srv_->run();
  1986. // check that the server did send a reposonce
  1987. ASSERT_EQ(1, srv_->fake_sent_.size());
  1988. // Make sure that we received a response
  1989. Pkt4Ptr adv = srv_->fake_sent_.front();
  1990. ASSERT_TRUE(adv);
  1991. // Get client-id...
  1992. OptionPtr clientid = adv->getOption(DHO_DHCP_SERVER_IDENTIFIER);
  1993. // ... and check if it is the modified value
  1994. OptionPtr expected = createOption(DHO_DHCP_SERVER_IDENTIFIER);
  1995. EXPECT_TRUE(clientid->equal(expected));
  1996. }
  1997. // Checks if callouts installed on pkt4_send is able to delete
  1998. // existing options and that server applies those changes. In particular,
  1999. // we are trying to send a packet without server-id. The packet should
  2000. // be sent
  2001. TEST_F(HooksDhcpv4SrvTest, pkt4SendDeleteServerId) {
  2002. IfaceMgrTestConfig test_config(true);
  2003. IfaceMgr::instance().openSockets4();
  2004. // Install pkt4_receive_callout
  2005. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  2006. "pkt4_send", pkt4_send_delete_serverid));
  2007. // Let's create a simple DISCOVER
  2008. Pkt4Ptr sol = generateSimpleDiscover();
  2009. // Simulate that we have received that traffic
  2010. srv_->fakeReceive(sol);
  2011. // Server will now process to run its normal loop, but instead of calling
  2012. // IfaceMgr::receive4(), it will read all packets from the list set by
  2013. // fakeReceive()
  2014. // In particular, it should call registered pkt4_receive callback.
  2015. srv_->run();
  2016. // Check that the server indeed sent a malformed ADVERTISE
  2017. ASSERT_EQ(1, srv_->fake_sent_.size());
  2018. // Get that ADVERTISE
  2019. Pkt4Ptr adv = srv_->fake_sent_.front();
  2020. ASSERT_TRUE(adv);
  2021. // Make sure that it does not have server-id
  2022. EXPECT_FALSE(adv->getOption(DHO_DHCP_SERVER_IDENTIFIER));
  2023. }
  2024. // Checks if callouts installed on pkt4_skip is able to set skip flag that
  2025. // will cause the server to not process the packet (drop), even though it is valid.
  2026. TEST_F(HooksDhcpv4SrvTest, skip_pkt4_send) {
  2027. IfaceMgrTestConfig test_config(true);
  2028. IfaceMgr::instance().openSockets4();
  2029. // Install pkt4_receive_callout
  2030. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  2031. "pkt4_send", pkt4_send_skip));
  2032. // Let's create a simple REQUEST
  2033. Pkt4Ptr sol = generateSimpleDiscover();
  2034. // Simulate that we have received that traffic
  2035. srv_->fakeReceive(sol);
  2036. // Server will now process to run its normal loop, but instead of calling
  2037. // IfaceMgr::receive4(), it will read all packets from the list set by
  2038. // fakeReceive()
  2039. // In particular, it should call registered pkt4_send callback.
  2040. srv_->run();
  2041. // Check that the server sent the message
  2042. ASSERT_EQ(1, srv_->fake_sent_.size());
  2043. // Get the first packet and check that it has zero length (i.e. the server
  2044. // did not do packing on its own)
  2045. Pkt4Ptr sent = srv_->fake_sent_.front();
  2046. EXPECT_EQ(0, sent->getBuffer().getLength());
  2047. }
  2048. // Checks if callouts installed on buffer4_send are indeed called and the
  2049. // all necessary parameters are passed.
  2050. TEST_F(HooksDhcpv4SrvTest, buffer4SendSimple) {
  2051. IfaceMgrTestConfig test_config(true);
  2052. IfaceMgr::instance().openSockets4();
  2053. // Install pkt4_receive_callout
  2054. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  2055. "buffer4_send", buffer4_send_callout));
  2056. // Let's create a simple DISCOVER
  2057. Pkt4Ptr discover = generateSimpleDiscover();
  2058. // Simulate that we have received that traffic
  2059. srv_->fakeReceive(discover);
  2060. // Server will now process to run its normal loop, but instead of calling
  2061. // IfaceMgr::receive4(), it will read all packets from the list set by
  2062. // fakeReceive()
  2063. // In particular, it should call registered pkt4_receive callback.
  2064. srv_->run();
  2065. // Check that the callback called is indeed the one we installed
  2066. EXPECT_EQ("buffer4_send", callback_name_);
  2067. // Check that there is one packet sent
  2068. ASSERT_EQ(1, srv_->fake_sent_.size());
  2069. Pkt4Ptr adv = srv_->fake_sent_.front();
  2070. // Check that pkt4 argument passing was successful and returned proper value
  2071. EXPECT_TRUE(callback_pkt4_.get() == adv.get());
  2072. // Check that all expected parameters are there
  2073. vector<string> expected_argument_names;
  2074. expected_argument_names.push_back(string("response4"));
  2075. EXPECT_TRUE(expected_argument_names == callback_argument_names_);
  2076. }
  2077. // Checks if callouts installed on buffer4_send are indeed called and that
  2078. // the output buffer can be changed.
  2079. TEST_F(HooksDhcpv4SrvTest, buffer4Send) {
  2080. IfaceMgrTestConfig test_config(true);
  2081. IfaceMgr::instance().openSockets4();
  2082. // Install pkt4_receive_callout
  2083. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  2084. "buffer4_send", buffer4_send_change_callout));
  2085. // Let's create a simple DISCOVER
  2086. Pkt4Ptr discover = generateSimpleDiscover();
  2087. // Simulate that we have received that traffic
  2088. srv_->fakeReceive(discover);
  2089. // Server will now process to run its normal loop, but instead of calling
  2090. // IfaceMgr::receive4(), it will read all packets from the list set by
  2091. // fakeReceive()
  2092. // In particular, it should call registered pkt4_receive callback.
  2093. srv_->run();
  2094. // Check that there is one packet sent
  2095. ASSERT_EQ(1, srv_->fake_sent_.size());
  2096. Pkt4Ptr adv = srv_->fake_sent_.front();
  2097. // The callout is supposed to fill the output buffer with dummyFile content
  2098. ASSERT_EQ(sizeof(dummyFile), adv->getBuffer().getLength());
  2099. EXPECT_EQ(0, memcmp(adv->getBuffer().getData(), dummyFile, sizeof(dummyFile)));
  2100. }
  2101. // Checks if callouts installed on buffer4_send can set skip flag and that flag
  2102. // causes the packet to not be sent
  2103. TEST_F(HooksDhcpv4SrvTest, buffer4SendSkip) {
  2104. IfaceMgrTestConfig test_config(true);
  2105. IfaceMgr::instance().openSockets4();
  2106. // Install pkt4_receive_callout
  2107. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  2108. "buffer4_send", skip_callout));
  2109. // Let's create a simple DISCOVER
  2110. Pkt4Ptr discover = generateSimpleDiscover();
  2111. // Simulate that we have received that traffic
  2112. srv_->fakeReceive(discover);
  2113. // Server will now process to run its normal loop, but instead of calling
  2114. // IfaceMgr::receive4(), it will read all packets from the list set by
  2115. // fakeReceive()
  2116. // In particular, it should call registered pkt4_receive callback.
  2117. srv_->run();
  2118. // Check that there is no packet sent.
  2119. ASSERT_EQ(0, srv_->fake_sent_.size());
  2120. }
  2121. // This test checks if subnet4_select callout is triggered and reports
  2122. // valid parameters
  2123. TEST_F(HooksDhcpv4SrvTest, subnet4SelectSimple) {
  2124. IfaceMgrTestConfig test_config(true);
  2125. IfaceMgr::instance().openSockets4();
  2126. // Install pkt4_receive_callout
  2127. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  2128. "subnet4_select", subnet4_select_callout));
  2129. // Configure 2 subnets, both directly reachable over local interface
  2130. // (let's not complicate the matter with relays)
  2131. string config = "{ \"interfaces\": [ \"*\" ],"
  2132. "\"rebind-timer\": 2000, "
  2133. "\"renew-timer\": 1000, "
  2134. "\"subnet4\": [ { "
  2135. " \"pool\": [ \"192.0.2.0/25\" ],"
  2136. " \"subnet\": \"192.0.2.0/24\", "
  2137. " \"interface\": \"eth0\" "
  2138. " }, {"
  2139. " \"pool\": [ \"192.0.3.0/25\" ],"
  2140. " \"subnet\": \"192.0.3.0/24\" "
  2141. " } ],"
  2142. "\"valid-lifetime\": 4000 }";
  2143. ElementPtr json = Element::fromJSON(config);
  2144. ConstElementPtr status;
  2145. // Configure the server and make sure the config is accepted
  2146. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2147. ASSERT_TRUE(status);
  2148. comment_ = config::parseAnswer(rcode_, status);
  2149. ASSERT_EQ(0, rcode_);
  2150. // Prepare discover packet. Server should select first subnet for it
  2151. Pkt4Ptr sol = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  2152. sol->setRemoteAddr(IOAddress("192.0.2.1"));
  2153. sol->setIface("eth1");
  2154. OptionPtr clientid = generateClientId();
  2155. sol->addOption(clientid);
  2156. // Pass it to the server and get an advertise
  2157. Pkt4Ptr adv = srv_->processDiscover(sol);
  2158. // check if we get response at all
  2159. ASSERT_TRUE(adv);
  2160. // Check that the callback called is indeed the one we installed
  2161. EXPECT_EQ("subnet4_select", callback_name_);
  2162. // Check that pkt4 argument passing was successful and returned proper value
  2163. EXPECT_TRUE(callback_pkt4_.get() == sol.get());
  2164. const Subnet4Collection* exp_subnets = CfgMgr::instance().getSubnets4();
  2165. // The server is supposed to pick the first subnet, because of matching
  2166. // interface. Check that the value is reported properly.
  2167. ASSERT_TRUE(callback_subnet4_);
  2168. EXPECT_EQ(exp_subnets->front().get(), callback_subnet4_.get());
  2169. // Server is supposed to report two subnets
  2170. ASSERT_EQ(exp_subnets->size(), callback_subnet4collection_->size());
  2171. // Compare that the available subnets are reported as expected
  2172. EXPECT_TRUE((*exp_subnets)[0].get() == (*callback_subnet4collection_)[0].get());
  2173. EXPECT_TRUE((*exp_subnets)[1].get() == (*callback_subnet4collection_)[1].get());
  2174. }
  2175. // This test checks if callout installed on subnet4_select hook point can pick
  2176. // a different subnet.
  2177. TEST_F(HooksDhcpv4SrvTest, subnet4SelectChange) {
  2178. IfaceMgrTestConfig test_config(true);
  2179. IfaceMgr::instance().openSockets4();
  2180. // Install a callout
  2181. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  2182. "subnet4_select", subnet4_select_different_subnet_callout));
  2183. // Configure 2 subnets, both directly reachable over local interface
  2184. // (let's not complicate the matter with relays)
  2185. string config = "{ \"interfaces\": [ \"*\" ],"
  2186. "\"rebind-timer\": 2000, "
  2187. "\"renew-timer\": 1000, "
  2188. "\"subnet4\": [ { "
  2189. " \"pool\": [ \"192.0.2.0/25\" ],"
  2190. " \"subnet\": \"192.0.2.0/24\", "
  2191. " \"interface\": \"eth0\" "
  2192. " }, {"
  2193. " \"pool\": [ \"192.0.3.0/25\" ],"
  2194. " \"subnet\": \"192.0.3.0/24\" "
  2195. " } ],"
  2196. "\"valid-lifetime\": 4000 }";
  2197. ElementPtr json = Element::fromJSON(config);
  2198. ConstElementPtr status;
  2199. // Configure the server and make sure the config is accepted
  2200. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  2201. ASSERT_TRUE(status);
  2202. comment_ = config::parseAnswer(rcode_, status);
  2203. ASSERT_EQ(0, rcode_);
  2204. // Prepare discover packet. Server should select first subnet for it
  2205. Pkt4Ptr sol = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  2206. sol->setRemoteAddr(IOAddress("192.0.2.1"));
  2207. sol->setIface("eth0");
  2208. OptionPtr clientid = generateClientId();
  2209. sol->addOption(clientid);
  2210. // Pass it to the server and get an advertise
  2211. Pkt4Ptr adv = srv_->processDiscover(sol);
  2212. // check if we get response at all
  2213. ASSERT_TRUE(adv);
  2214. // The response should have an address from second pool, so let's check it
  2215. IOAddress addr = adv->getYiaddr();
  2216. EXPECT_NE("0.0.0.0", addr.toText());
  2217. // Get all subnets and use second subnet for verification
  2218. const Subnet4Collection* subnets = CfgMgr::instance().getSubnets4();
  2219. ASSERT_EQ(2, subnets->size());
  2220. // Advertised address must belong to the second pool (in subnet's range,
  2221. // in dynamic pool)
  2222. EXPECT_TRUE((*subnets)[1]->inRange(addr));
  2223. EXPECT_TRUE((*subnets)[1]->inPool(Lease::TYPE_V4, addr));
  2224. }
  2225. // This test verifies that incoming (positive) REQUEST/Renewing can be handled
  2226. // properly and that callout installed on lease4_renew is triggered with
  2227. // expected parameters.
  2228. TEST_F(HooksDhcpv4SrvTest, lease4RenewSimple) {
  2229. IfaceMgrTestConfig test_config(true);
  2230. IfaceMgr::instance().openSockets4();
  2231. const IOAddress addr("192.0.2.106");
  2232. const uint32_t temp_t1 = 50;
  2233. const uint32_t temp_t2 = 75;
  2234. const uint32_t temp_valid = 100;
  2235. const time_t temp_timestamp = time(NULL) - 10;
  2236. // Install a callout
  2237. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  2238. "lease4_renew", lease4_renew_callout));
  2239. // Generate client-id also sets client_id_ member
  2240. OptionPtr clientid = generateClientId();
  2241. // Check that the address we are about to use is indeed in pool
  2242. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
  2243. // let's create a lease and put it in the LeaseMgr
  2244. uint8_t hwaddr2[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  2245. Lease4Ptr used(new Lease4(IOAddress("192.0.2.106"), hwaddr2, sizeof(hwaddr2),
  2246. &client_id_->getDuid()[0], client_id_->getDuid().size(),
  2247. temp_valid, temp_t1, temp_t2, temp_timestamp,
  2248. subnet_->getID()));
  2249. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  2250. // Check that the lease is really in the database
  2251. Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
  2252. ASSERT_TRUE(l);
  2253. // Let's create a RENEW
  2254. Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  2255. req->setRemoteAddr(IOAddress(addr));
  2256. req->setYiaddr(addr);
  2257. req->setCiaddr(addr); // client's address
  2258. req->setIface("eth0");
  2259. req->addOption(clientid);
  2260. req->addOption(srv_->getServerID());
  2261. // Pass it to the server and hope for a REPLY
  2262. Pkt4Ptr ack = srv_->processRequest(req);
  2263. // Check if we get response at all
  2264. checkResponse(ack, DHCPACK, 1234);
  2265. // Check that the lease is really in the database
  2266. l = checkLease(ack, clientid, req->getHWAddr(), addr);
  2267. ASSERT_TRUE(l);
  2268. // Check that T1, T2, preferred, valid and cltt were really updated
  2269. EXPECT_EQ(l->t1_, subnet_->getT1());
  2270. EXPECT_EQ(l->t2_, subnet_->getT2());
  2271. EXPECT_EQ(l->valid_lft_, subnet_->getValid());
  2272. // Check that the callback called is indeed the one we installed
  2273. EXPECT_EQ("lease4_renew", callback_name_);
  2274. // Check that hwaddr parameter is passed properly
  2275. ASSERT_TRUE(callback_hwaddr_);
  2276. EXPECT_TRUE(*callback_hwaddr_ == *req->getHWAddr());
  2277. // Check that the subnet is passed properly
  2278. ASSERT_TRUE(callback_subnet4_);
  2279. EXPECT_EQ(callback_subnet4_->toText(), subnet_->toText());
  2280. ASSERT_TRUE(callback_clientid_);
  2281. ASSERT_TRUE(client_id_);
  2282. EXPECT_TRUE(*client_id_ == *callback_clientid_);
  2283. // Check if all expected parameters were really received
  2284. vector<string> expected_argument_names;
  2285. expected_argument_names.push_back("subnet4");
  2286. expected_argument_names.push_back("clientid");
  2287. expected_argument_names.push_back("hwaddr");
  2288. expected_argument_names.push_back("lease4");
  2289. sort(callback_argument_names_.begin(), callback_argument_names_.end());
  2290. sort(expected_argument_names.begin(), expected_argument_names.end());
  2291. EXPECT_TRUE(callback_argument_names_ == expected_argument_names);
  2292. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  2293. }
  2294. // This test verifies that a callout installed on lease4_renew can trigger
  2295. // the server to not renew a lease.
  2296. TEST_F(HooksDhcpv4SrvTest, lease4RenewSkip) {
  2297. IfaceMgrTestConfig test_config(true);
  2298. IfaceMgr::instance().openSockets4();
  2299. const IOAddress addr("192.0.2.106");
  2300. const uint32_t temp_t1 = 50;
  2301. const uint32_t temp_t2 = 75;
  2302. const uint32_t temp_valid = 100;
  2303. const time_t temp_timestamp = time(NULL) - 10;
  2304. // Install a callout
  2305. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  2306. "lease4_renew", skip_callout));
  2307. // Generate client-id also sets client_id_ member
  2308. OptionPtr clientid = generateClientId();
  2309. // Check that the address we are about to use is indeed in pool
  2310. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
  2311. // let's create a lease and put it in the LeaseMgr
  2312. uint8_t hwaddr2[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  2313. Lease4Ptr used(new Lease4(IOAddress("192.0.2.106"), hwaddr2, sizeof(hwaddr2),
  2314. &client_id_->getDuid()[0], client_id_->getDuid().size(),
  2315. temp_valid, temp_t1, temp_t2, temp_timestamp,
  2316. subnet_->getID()));
  2317. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  2318. // Check that the lease is really in the database
  2319. Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
  2320. ASSERT_TRUE(l);
  2321. // Check that T1, T2, preferred, valid and cltt really set.
  2322. // Constructed lease looks as if it was assigned 10 seconds ago
  2323. // EXPECT_EQ(l->t1_, temp_t1);
  2324. // EXPECT_EQ(l->t2_, temp_t2);
  2325. EXPECT_EQ(l->valid_lft_, temp_valid);
  2326. EXPECT_EQ(l->cltt_, temp_timestamp);
  2327. // Let's create a RENEW
  2328. Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
  2329. req->setRemoteAddr(IOAddress(addr));
  2330. req->setYiaddr(addr);
  2331. req->setCiaddr(addr); // client's address
  2332. req->setIface("eth0");
  2333. req->addOption(clientid);
  2334. req->addOption(srv_->getServerID());
  2335. // Pass it to the server and hope for a REPLY
  2336. Pkt4Ptr ack = srv_->processRequest(req);
  2337. ASSERT_TRUE(ack);
  2338. // Check that the lease is really in the database
  2339. l = checkLease(ack, clientid, req->getHWAddr(), addr);
  2340. ASSERT_TRUE(l);
  2341. // Check that T1, T2, valid and cltt were NOT updated
  2342. EXPECT_EQ(temp_t1, l->t1_);
  2343. EXPECT_EQ(temp_t2, l->t2_);
  2344. EXPECT_EQ(temp_valid, l->valid_lft_);
  2345. EXPECT_EQ(temp_timestamp, l->cltt_);
  2346. EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
  2347. }
  2348. // This test verifies that valid RELEASE triggers lease4_release callouts
  2349. TEST_F(HooksDhcpv4SrvTest, lease4ReleaseSimple) {
  2350. IfaceMgrTestConfig test_config(true);
  2351. IfaceMgr::instance().openSockets4();
  2352. const IOAddress addr("192.0.2.106");
  2353. const uint32_t temp_t1 = 50;
  2354. const uint32_t temp_t2 = 75;
  2355. const uint32_t temp_valid = 100;
  2356. const time_t temp_timestamp = time(NULL) - 10;
  2357. // Install a callout
  2358. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  2359. "lease4_release", lease4_release_callout));
  2360. // Generate client-id also duid_
  2361. OptionPtr clientid = generateClientId();
  2362. // Check that the address we are about to use is indeed in pool
  2363. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
  2364. // Let's create a lease and put it in the LeaseMgr
  2365. uint8_t mac_addr[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  2366. HWAddrPtr hw(new HWAddr(mac_addr, sizeof(mac_addr), HTYPE_ETHER));
  2367. Lease4Ptr used(new Lease4(addr, mac_addr, sizeof(mac_addr),
  2368. &client_id_->getDuid()[0], client_id_->getDuid().size(),
  2369. temp_valid, temp_t1, temp_t2, temp_timestamp,
  2370. subnet_->getID()));
  2371. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  2372. // Check that the lease is really in the database
  2373. Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
  2374. ASSERT_TRUE(l);
  2375. // Let's create a RELEASE
  2376. // Generate client-id also duid_
  2377. Pkt4Ptr rel = Pkt4Ptr(new Pkt4(DHCPRELEASE, 1234));
  2378. rel->setRemoteAddr(addr);
  2379. rel->setCiaddr(addr);
  2380. rel->addOption(clientid);
  2381. rel->addOption(srv_->getServerID());
  2382. rel->setHWAddr(hw);
  2383. // Pass it to the server and hope for a REPLY
  2384. // Note: this is no response to RELEASE in DHCPv4
  2385. EXPECT_NO_THROW(srv_->processRelease(rel));
  2386. // The lease should be gone from LeaseMgr
  2387. l = LeaseMgrFactory::instance().getLease4(addr);
  2388. EXPECT_FALSE(l);
  2389. // Try to get the lease by hardware address
  2390. // @todo: Uncomment this once trac2592 is implemented
  2391. // Lease4Collection leases = LeaseMgrFactory::instance().getLease4(hw->hwaddr_);
  2392. // EXPECT_EQ(leases.size(), 0);
  2393. // Try to get it by hw/subnet_id combination
  2394. l = LeaseMgrFactory::instance().getLease4(hw->hwaddr_, subnet_->getID());
  2395. EXPECT_FALSE(l);
  2396. // Try by client-id
  2397. // @todo: Uncomment this once trac2592 is implemented
  2398. //Lease4Collection leases = LeaseMgrFactory::instance().getLease4(*client_id_);
  2399. //EXPECT_EQ(leases.size(), 0);
  2400. // Try by client-id/subnet-id
  2401. l = LeaseMgrFactory::instance().getLease4(*client_id_, subnet_->getID());
  2402. EXPECT_FALSE(l);
  2403. // Ok, the lease is *really* not there.
  2404. // Check that the callback called is indeed the one we installed
  2405. EXPECT_EQ("lease4_release", callback_name_);
  2406. // Check that pkt4 argument passing was successful and returned proper value
  2407. EXPECT_TRUE(callback_pkt4_.get() == rel.get());
  2408. // Check if all expected parameters were really received
  2409. vector<string> expected_argument_names;
  2410. expected_argument_names.push_back("query4");
  2411. expected_argument_names.push_back("lease4");
  2412. sort(callback_argument_names_.begin(), callback_argument_names_.end());
  2413. sort(expected_argument_names.begin(), expected_argument_names.end());
  2414. EXPECT_TRUE(callback_argument_names_ == expected_argument_names);
  2415. }
  2416. // This test verifies that skip flag returned by a callout installed on the
  2417. // lease4_release hook point will keep the lease
  2418. TEST_F(HooksDhcpv4SrvTest, lease4ReleaseSkip) {
  2419. IfaceMgrTestConfig test_config(true);
  2420. IfaceMgr::instance().openSockets4();
  2421. const IOAddress addr("192.0.2.106");
  2422. const uint32_t temp_t1 = 50;
  2423. const uint32_t temp_t2 = 75;
  2424. const uint32_t temp_valid = 100;
  2425. const time_t temp_timestamp = time(NULL) - 10;
  2426. // Install a callout
  2427. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  2428. "lease4_release", skip_callout));
  2429. // Generate client-id also duid_
  2430. OptionPtr clientid = generateClientId();
  2431. // Check that the address we are about to use is indeed in pool
  2432. ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
  2433. // Let's create a lease and put it in the LeaseMgr
  2434. uint8_t mac_addr[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  2435. HWAddrPtr hw(new HWAddr(mac_addr, sizeof(mac_addr), HTYPE_ETHER));
  2436. Lease4Ptr used(new Lease4(addr, mac_addr, sizeof(mac_addr),
  2437. &client_id_->getDuid()[0], client_id_->getDuid().size(),
  2438. temp_valid, temp_t1, temp_t2, temp_timestamp,
  2439. subnet_->getID()));
  2440. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  2441. // Check that the lease is really in the database
  2442. Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
  2443. ASSERT_TRUE(l);
  2444. // Let's create a RELEASE
  2445. // Generate client-id also duid_
  2446. Pkt4Ptr rel = Pkt4Ptr(new Pkt4(DHCPRELEASE, 1234));
  2447. rel->setRemoteAddr(addr);
  2448. rel->setYiaddr(addr);
  2449. rel->addOption(clientid);
  2450. rel->addOption(srv_->getServerID());
  2451. rel->setHWAddr(hw);
  2452. // Pass it to the server and hope for a REPLY
  2453. // Note: this is no response to RELEASE in DHCPv4
  2454. EXPECT_NO_THROW(srv_->processRelease(rel));
  2455. // The lease should be still there
  2456. l = LeaseMgrFactory::instance().getLease4(addr);
  2457. EXPECT_TRUE(l);
  2458. // Try by client-id/subnet-id
  2459. l = LeaseMgrFactory::instance().getLease4(*client_id_, subnet_->getID());
  2460. EXPECT_TRUE(l);
  2461. // Try to get the lease by hardware address
  2462. // @todo: Uncomment this once trac2592 is implemented
  2463. // Lease4Collection leases = LeaseMgrFactory::instance().getLease4(hw->hwaddr_);
  2464. // EXPECT_EQ(leases.size(), 1);
  2465. // Try by client-id
  2466. // @todo: Uncomment this once trac2592 is implemented
  2467. //Lease4Collection leases = LeaseMgrFactory::instance().getLease4(*client_id_);
  2468. //EXPECT_EQ(leases.size(), 1);
  2469. }
  2470. // Checks if server is able to handle a relayed traffic from DOCSIS3.0 modems
  2471. TEST_F(Dhcpv4SrvTest, docsisVendorOptionsParse) {
  2472. // Let's get a traffic capture from DOCSIS3.0 modem
  2473. Pkt4Ptr dis = captureRelayedDiscover();
  2474. ASSERT_NO_THROW(dis->unpack());
  2475. // Check if the packet contain
  2476. OptionPtr opt = dis->getOption(DHO_VIVSO_SUBOPTIONS);
  2477. ASSERT_TRUE(opt);
  2478. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  2479. ASSERT_TRUE(vendor);
  2480. // This particular capture that we have included options 1 and 5
  2481. EXPECT_TRUE(vendor->getOption(1));
  2482. EXPECT_TRUE(vendor->getOption(5));
  2483. // It did not include options any other options
  2484. EXPECT_FALSE(vendor->getOption(2));
  2485. EXPECT_FALSE(vendor->getOption(3));
  2486. EXPECT_FALSE(vendor->getOption(17));
  2487. }
  2488. // Checks if server is able to parse incoming docsis option and extract suboption 1 (docsis ORO)
  2489. TEST_F(Dhcpv4SrvTest, docsisVendorORO) {
  2490. // Let's get a traffic capture from DOCSIS3.0 modem
  2491. Pkt4Ptr dis = captureRelayedDiscover();
  2492. EXPECT_NO_THROW(dis->unpack());
  2493. // Check if the packet contains vendor specific information option
  2494. OptionPtr opt = dis->getOption(DHO_VIVSO_SUBOPTIONS);
  2495. ASSERT_TRUE(opt);
  2496. boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  2497. ASSERT_TRUE(vendor);
  2498. opt = vendor->getOption(DOCSIS3_V4_ORO);
  2499. ASSERT_TRUE(opt);
  2500. OptionUint8ArrayPtr oro = boost::dynamic_pointer_cast<OptionUint8Array>(opt);
  2501. EXPECT_TRUE(oro);
  2502. }
  2503. // This test checks if Option Request Option (ORO) in docsis (vendor-id=4491)
  2504. // vendor options is parsed correctly and the requested options are actually assigned.
  2505. TEST_F(Dhcpv4SrvTest, vendorOptionsORO) {
  2506. IfaceMgrTestConfig test_config(true);
  2507. IfaceMgr::instance().openSockets4();
  2508. NakedDhcpv4Srv srv(0);
  2509. ConstElementPtr x;
  2510. string config = "{ \"interfaces\": [ \"all\" ],"
  2511. "\"rebind-timer\": 2000, "
  2512. "\"renew-timer\": 1000, "
  2513. " \"option-data\": [ {"
  2514. " \"name\": \"tftp-servers\","
  2515. " \"space\": \"vendor-4491\","
  2516. " \"code\": 2,"
  2517. " \"data\": \"192.0.2.1, 192.0.2.2\","
  2518. " \"csv-format\": True"
  2519. " }],"
  2520. "\"subnet4\": [ { "
  2521. " \"pool\": [ \"192.0.2.0/25\" ],"
  2522. " \"subnet\": \"192.0.2.0/24\", "
  2523. " \"rebind-timer\": 2000, "
  2524. " \"renew-timer\": 1000, "
  2525. " \"valid-lifetime\": 4000,"
  2526. " \"interface\": \"eth0\" "
  2527. " } ],"
  2528. "\"valid-lifetime\": 4000 }";
  2529. ElementPtr json = Element::fromJSON(config);
  2530. EXPECT_NO_THROW(x = configureDhcp4Server(srv, json));
  2531. ASSERT_TRUE(x);
  2532. comment_ = isc::config::parseAnswer(rcode_, x);
  2533. ASSERT_EQ(0, rcode_);
  2534. boost::shared_ptr<Pkt4> dis(new Pkt4(DHCPDISCOVER, 1234));
  2535. // Set the giaddr and hops to non-zero address as if it was relayed.
  2536. dis->setGiaddr(IOAddress("192.0.2.1"));
  2537. dis->setHops(1);
  2538. OptionPtr clientid = generateClientId();
  2539. dis->addOption(clientid);
  2540. // Set interface. It is required by the server to generate server id.
  2541. dis->setIface("eth0");
  2542. // Pass it to the server and get an advertise
  2543. Pkt4Ptr offer = srv.processDiscover(dis);
  2544. // check if we get response at all
  2545. ASSERT_TRUE(offer);
  2546. // We did not include any vendor opts in DISCOVER, so there should be none
  2547. // in OFFER.
  2548. ASSERT_FALSE(offer->getOption(DHO_VIVSO_SUBOPTIONS));
  2549. // Let's add a vendor-option (vendor-id=4491) with a single sub-option.
  2550. // That suboption has code 1 and is a docsis ORO option.
  2551. boost::shared_ptr<OptionUint8Array> vendor_oro(new OptionUint8Array(Option::V4,
  2552. DOCSIS3_V4_ORO));
  2553. vendor_oro->addValue(DOCSIS3_V4_TFTP_SERVERS); // Request option 33
  2554. OptionPtr vendor(new OptionVendor(Option::V4, 4491));
  2555. vendor->addOption(vendor_oro);
  2556. dis->addOption(vendor);
  2557. // Need to process SOLICIT again after requesting new option.
  2558. offer = srv.processDiscover(dis);
  2559. ASSERT_TRUE(offer);
  2560. // Check if thre is vendor option response
  2561. OptionPtr tmp = offer->getOption(DHO_VIVSO_SUBOPTIONS);
  2562. ASSERT_TRUE(tmp);
  2563. // The response should be OptionVendor object
  2564. boost::shared_ptr<OptionVendor> vendor_resp =
  2565. boost::dynamic_pointer_cast<OptionVendor>(tmp);
  2566. ASSERT_TRUE(vendor_resp);
  2567. OptionPtr docsis2 = vendor_resp->getOption(DOCSIS3_V4_TFTP_SERVERS);
  2568. ASSERT_TRUE(docsis2);
  2569. Option4AddrLstPtr tftp_srvs = boost::dynamic_pointer_cast<Option4AddrLst>(docsis2);
  2570. ASSERT_TRUE(tftp_srvs);
  2571. Option4AddrLst::AddressContainer addrs = tftp_srvs->getAddresses();
  2572. ASSERT_EQ(2, addrs.size());
  2573. EXPECT_EQ("192.0.2.1", addrs[0].toText());
  2574. EXPECT_EQ("192.0.2.2", addrs[1].toText());
  2575. }
  2576. // Test checks whether it is possible to use option definitions defined in
  2577. // src/lib/dhcp/docsis3_option_defs.h.
  2578. TEST_F(Dhcpv4SrvTest, vendorOptionsDocsisDefinitions) {
  2579. ConstElementPtr x;
  2580. string config_prefix = "{ \"interfaces\": [ \"all\" ],"
  2581. "\"rebind-timer\": 2000, "
  2582. "\"renew-timer\": 1000, "
  2583. " \"option-data\": [ {"
  2584. " \"name\": \"tftp-servers\","
  2585. " \"space\": \"vendor-4491\","
  2586. " \"code\": ";
  2587. string config_postfix = ","
  2588. " \"data\": \"192.0.2.1\","
  2589. " \"csv-format\": True"
  2590. " }],"
  2591. "\"subnet4\": [ { "
  2592. " \"pool\": [ \"192.0.2.1 - 192.0.2.50\" ],"
  2593. " \"subnet\": \"192.0.2.0/24\", "
  2594. " \"renew-timer\": 1000, "
  2595. " \"rebind-timer\": 1000, "
  2596. " \"valid-lifetime\": 4000,"
  2597. " \"interface\": \"\""
  2598. " } ],"
  2599. "\"valid-lifetime\": 4000 }";
  2600. // There is docsis3 (vendor-id=4491) vendor option 2, which is a
  2601. // tftp-server. Its format is list of IPv4 addresses.
  2602. string config_valid = config_prefix + "2" + config_postfix;
  2603. // There is no option 99 defined in vendor-id=4491. As there is no
  2604. // definition, the config should fail.
  2605. string config_bogus = config_prefix + "99" + config_postfix;
  2606. ElementPtr json_bogus = Element::fromJSON(config_bogus);
  2607. ElementPtr json_valid = Element::fromJSON(config_valid);
  2608. NakedDhcpv4Srv srv(0);
  2609. // This should fail (missing option definition)
  2610. EXPECT_NO_THROW(x = configureDhcp4Server(srv, json_bogus));
  2611. ASSERT_TRUE(x);
  2612. comment_ = isc::config::parseAnswer(rcode_, x);
  2613. ASSERT_EQ(1, rcode_);
  2614. // This should work (option definition present)
  2615. EXPECT_NO_THROW(x = configureDhcp4Server(srv, json_valid));
  2616. ASSERT_TRUE(x);
  2617. comment_ = isc::config::parseAnswer(rcode_, x);
  2618. ASSERT_EQ(0, rcode_);
  2619. }
  2620. // Checks if client packets are classified properly
  2621. TEST_F(Dhcpv4SrvTest, clientClassification) {
  2622. NakedDhcpv4Srv srv(0);
  2623. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  2624. // vendor-class set to docsis3.0
  2625. Pkt4Ptr dis1;
  2626. ASSERT_NO_THROW(dis1 = captureRelayedDiscover());
  2627. ASSERT_NO_THROW(dis1->unpack());
  2628. srv.classifyPacket(dis1);
  2629. EXPECT_TRUE(dis1->inClass(srv.VENDOR_CLASS_PREFIX + "docsis3.0"));
  2630. EXPECT_FALSE(dis1->inClass(srv.VENDOR_CLASS_PREFIX + "eRouter1.0"));
  2631. // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
  2632. // vendor-class set to eRouter1.0
  2633. Pkt4Ptr dis2;
  2634. ASSERT_NO_THROW(dis2 = captureRelayedDiscover2());
  2635. ASSERT_NO_THROW(dis2->unpack());
  2636. srv.classifyPacket(dis2);
  2637. EXPECT_TRUE(dis2->inClass(srv.VENDOR_CLASS_PREFIX + "eRouter1.0"));
  2638. EXPECT_FALSE(dis2->inClass(srv.VENDOR_CLASS_PREFIX + "docsis3.0"));
  2639. }
  2640. // Checks if the client-class field is indeed used for subnet selection.
  2641. // Note that packet classification is already checked in Dhcpv4SrvTest
  2642. // .clientClassification above.
  2643. TEST_F(Dhcpv4SrvTest, clientClassify2) {
  2644. // This test configures 2 subnets. We actually only need the
  2645. // first one, but since there's still this ugly hack that picks
  2646. // the pool if there is only one, we must use more than one
  2647. // subnet. That ugly hack will be removed in #3242, currently
  2648. // under review.
  2649. // The second subnet does not play any role here. The client's
  2650. // IP address belongs to the first subnet, so only that first
  2651. // subnet it being tested.
  2652. string config = "{ \"interfaces\": [ \"*\" ],"
  2653. "\"rebind-timer\": 2000, "
  2654. "\"renew-timer\": 1000, "
  2655. "\"subnet4\": [ "
  2656. "{ \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  2657. " \"client-class\": \"foo\", "
  2658. " \"subnet\": \"192.0.2.0/24\" }, "
  2659. "{ \"pool\": [ \"192.0.3.1 - 192.0.3.100\" ],"
  2660. " \"client-class\": \"xyzzy\", "
  2661. " \"subnet\": \"192.0.3.0/24\" } "
  2662. "],"
  2663. "\"valid-lifetime\": 4000 }";
  2664. ASSERT_NO_THROW(configure(config));
  2665. // Create a simple packet that we'll use for classification
  2666. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  2667. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  2668. dis->setCiaddr(IOAddress("192.0.2.1"));
  2669. dis->setIface("eth0");
  2670. OptionPtr clientid = generateClientId();
  2671. dis->addOption(clientid);
  2672. // This discover does not belong to foo class, so it will not
  2673. // be serviced
  2674. EXPECT_FALSE(srv_.selectSubnet(dis));
  2675. // Let's add the packet to bar class and try again.
  2676. dis->addClass("bar");
  2677. // Still not supported, because it belongs to wrong class.
  2678. EXPECT_FALSE(srv_.selectSubnet(dis));
  2679. // Let's add it to maching class.
  2680. dis->addClass("foo");
  2681. // This time it should work
  2682. EXPECT_TRUE(srv_.selectSubnet(dis));
  2683. }
  2684. // Checks if relay IP address specified in the relay-info structure in
  2685. // subnet4 is being used properly.
  2686. TEST_F(Dhcpv4SrvTest, relayOverride) {
  2687. // We have 2 subnets defined. Note that both have a relay address
  2688. // defined. Both are not belonging to the subnets. That is
  2689. // important, because if the relay belongs to the subnet, there's
  2690. // no need to specify relay override.
  2691. string config = "{ \"interfaces\": [ \"*\" ],"
  2692. "\"rebind-timer\": 2000, "
  2693. "\"renew-timer\": 1000, "
  2694. "\"subnet4\": [ "
  2695. "{ \"pool\": [ \"192.0.2.2 - 192.0.2.100\" ],"
  2696. " \"relay\": { "
  2697. " \"ip-address\": \"192.0.5.1\""
  2698. " },"
  2699. " \"subnet\": \"192.0.2.0/24\" }, "
  2700. "{ \"pool\": [ \"192.0.3.1 - 192.0.3.100\" ],"
  2701. " \"relay\": { "
  2702. " \"ip-address\": \"192.0.5.2\""
  2703. " },"
  2704. " \"subnet\": \"192.0.3.0/24\" } "
  2705. "],"
  2706. "\"valid-lifetime\": 4000 }";
  2707. // Use this config to set up the server
  2708. ASSERT_NO_THROW(configure(config));
  2709. // Let's get the subnet configuration objects
  2710. const Subnet4Collection* subnets = CfgMgr::instance().getSubnets4();
  2711. ASSERT_EQ(2, subnets->size());
  2712. // Let's get them for easy reference
  2713. Subnet4Ptr subnet1 = (*subnets)[0];
  2714. Subnet4Ptr subnet2 = (*subnets)[1];
  2715. ASSERT_TRUE(subnet1);
  2716. ASSERT_TRUE(subnet2);
  2717. // Let's create a packet.
  2718. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  2719. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  2720. dis->setIface("eth0");
  2721. dis->setHops(1);
  2722. OptionPtr clientid = generateClientId();
  2723. dis->addOption(clientid);
  2724. // This is just a sanity check, we're using regular method: ciaddr 192.0.2.1
  2725. // belongs to the first subnet, so it is selected
  2726. dis->setGiaddr(IOAddress("192.0.2.1"));
  2727. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  2728. // Relay belongs to the second subnet, so it should be selected.
  2729. dis->setGiaddr(IOAddress("192.0.3.1"));
  2730. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2731. // Now let's check if the relay override for the first subnets works
  2732. dis->setGiaddr(IOAddress("192.0.5.1"));
  2733. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  2734. // The same check for the second subnet...
  2735. dis->setGiaddr(IOAddress("192.0.5.2"));
  2736. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2737. // And finally, let's check if mis-matched relay address will end up
  2738. // in not selecting a subnet at all
  2739. dis->setGiaddr(IOAddress("192.0.5.3"));
  2740. EXPECT_FALSE(srv_.selectSubnet(dis));
  2741. // Finally, check that the relay override works only with relay address
  2742. // (GIADDR) and does not affect client address (CIADDR)
  2743. dis->setGiaddr(IOAddress("0.0.0.0"));
  2744. dis->setHops(0);
  2745. dis->setCiaddr(IOAddress("192.0.5.1"));
  2746. EXPECT_FALSE(srv_.selectSubnet(dis));
  2747. }
  2748. // Checks if relay IP address specified in the relay-info structure can be
  2749. // used together with client-classification.
  2750. TEST_F(Dhcpv4SrvTest, relayOverrideAndClientClass) {
  2751. // This test configures 2 subnets. They both are on the same link, so they
  2752. // have the same relay-ip address. Furthermore, the first subnet is
  2753. // reserved for clients that belong to class "foo".
  2754. string config = "{ \"interfaces\": [ \"*\" ],"
  2755. "\"rebind-timer\": 2000, "
  2756. "\"renew-timer\": 1000, "
  2757. "\"subnet4\": [ "
  2758. "{ \"pool\": [ \"192.0.2.2 - 192.0.2.100\" ],"
  2759. " \"client-class\": \"foo\", "
  2760. " \"relay\": { "
  2761. " \"ip-address\": \"192.0.5.1\""
  2762. " },"
  2763. " \"subnet\": \"192.0.2.0/24\" }, "
  2764. "{ \"pool\": [ \"192.0.3.1 - 192.0.3.100\" ],"
  2765. " \"relay\": { "
  2766. " \"ip-address\": \"192.0.5.1\""
  2767. " },"
  2768. " \"subnet\": \"192.0.3.0/24\" } "
  2769. "],"
  2770. "\"valid-lifetime\": 4000 }";
  2771. // Use this config to set up the server
  2772. ASSERT_NO_THROW(configure(config));
  2773. const Subnet4Collection* subnets = CfgMgr::instance().getSubnets4();
  2774. ASSERT_EQ(2, subnets->size());
  2775. // Let's get them for easy reference
  2776. Subnet4Ptr subnet1 = (*subnets)[0];
  2777. Subnet4Ptr subnet2 = (*subnets)[1];
  2778. ASSERT_TRUE(subnet1);
  2779. ASSERT_TRUE(subnet2);
  2780. // Let's create a packet.
  2781. Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
  2782. dis->setRemoteAddr(IOAddress("192.0.2.1"));
  2783. dis->setIface("eth0");
  2784. dis->setHops(1);
  2785. dis->setGiaddr(IOAddress("192.0.5.1"));
  2786. OptionPtr clientid = generateClientId();
  2787. dis->addOption(clientid);
  2788. // This packet does not belong to class foo, so it should be rejected in
  2789. // subnet[0], even though the relay-ip matches. It should be accepted in
  2790. // subnet[1], because the subnet matches and there are no class
  2791. // requirements.
  2792. EXPECT_TRUE(subnet2 == srv_.selectSubnet(dis));
  2793. // Now let's add this packet to class foo and recheck. This time it should
  2794. // be accepted in the first subnet, because both class and relay-ip match.
  2795. dis->addClass("foo");
  2796. EXPECT_TRUE(subnet1 == srv_.selectSubnet(dis));
  2797. }
  2798. // This test verifies that the direct message is dropped when it has been
  2799. // received by the server via an interface for which there is no subnet
  2800. // configured. It also checks that the message is not dropped (is processed)
  2801. // when it is relayed or unicast.
  2802. TEST_F(Dhcpv4SrvTest, acceptDirectRequest) {
  2803. IfaceMgrTestConfig test_config(true);
  2804. IfaceMgr::instance().openSockets4();
  2805. NakedDhcpv4Srv srv(0);
  2806. Pkt4Ptr pkt(new Pkt4(DHCPDISCOVER, 1234));
  2807. // Set Giaddr and local server's unicast address, but don't set hops.
  2808. // Hops value must be greater than 0, when giaddr is set. Otherwise,
  2809. // message is considered malformed and the accept() function should
  2810. // return false.
  2811. pkt->setGiaddr(IOAddress("192.0.10.1"));
  2812. pkt->setLocalAddr(IOAddress("192.0.2.3"));
  2813. pkt->setIface("eth1");
  2814. EXPECT_FALSE(srv.accept(pkt));
  2815. // Let's set hops and check that the message is now accepted as
  2816. // a relayed message.
  2817. pkt->setHops(1);
  2818. EXPECT_TRUE(srv.accept(pkt));
  2819. // Make it a direct message but keep unicast server's address. The
  2820. // messages sent to unicast address should be accepted as they are
  2821. // most likely to renew existing leases. The server should respond
  2822. // to renews so they have to be accepted and processed.
  2823. pkt->setHops(0);
  2824. pkt->setGiaddr(IOAddress("0.0.0.0"));
  2825. EXPECT_TRUE(srv.accept(pkt));
  2826. // Direct message is now sent to a broadcast address. The server
  2827. // should accept this message because it has been received via
  2828. // eth1 for which there is a subnet configured (see test fixture
  2829. // class constructor).
  2830. pkt->setLocalAddr(IOAddress("255.255.255.255"));
  2831. EXPECT_TRUE(srv.accept(pkt));
  2832. // For eth0, there is no subnet configured. Such message is expected
  2833. // to be silently dropped.
  2834. pkt->setIface("eth0");
  2835. EXPECT_FALSE(srv.accept(pkt));
  2836. // But, if the message is unicast it should be accepted, even though
  2837. // it has been received via eth0.
  2838. pkt->setLocalAddr(IOAddress("10.0.0.1"));
  2839. EXPECT_TRUE(srv.accept(pkt));
  2840. }
  2841. // This test checks that the server rejects a message with invalid type.
  2842. TEST_F(Dhcpv4SrvTest, acceptMessageType) {
  2843. IfaceMgrTestConfig test_config(true);
  2844. IfaceMgr::instance().openSockets4();
  2845. NakedDhcpv4Srv srv(0);
  2846. // Specify messages to be accepted by the server.
  2847. int allowed[] = {
  2848. DHCPDISCOVER,
  2849. DHCPREQUEST,
  2850. DHCPRELEASE,
  2851. DHCPDECLINE,
  2852. DHCPINFORM
  2853. };
  2854. size_t allowed_size = sizeof(allowed) / sizeof(allowed[0]);
  2855. // Check that the server actually accepts these message types.
  2856. for (int i = 0; i < allowed_size; ++i) {
  2857. EXPECT_TRUE(srv.acceptMessageType(Pkt4Ptr(new Pkt4(allowed[i], 1234))))
  2858. << "Test failed for message type " << i;
  2859. }
  2860. // Specify messages which server is supposed to drop.
  2861. int not_allowed[] = {
  2862. DHCPOFFER,
  2863. DHCPACK,
  2864. DHCPNAK,
  2865. DHCPLEASEQUERY,
  2866. DHCPLEASEUNASSIGNED,
  2867. DHCPLEASEUNKNOWN,
  2868. DHCPLEASEACTIVE,
  2869. DHCPBULKLEASEQUERY,
  2870. DHCPLEASEQUERYDONE
  2871. };
  2872. size_t not_allowed_size = sizeof(not_allowed) / sizeof(not_allowed[0]);
  2873. // Actually check that the server will drop these messages.
  2874. for (int i = 0; i < not_allowed_size; ++i) {
  2875. EXPECT_FALSE(srv.acceptMessageType(Pkt4Ptr(new Pkt4(not_allowed[i],
  2876. 1234))))
  2877. << "Test failed for message type " << i;
  2878. }
  2879. }
  2880. }; // end of anonymous namespace