123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952 |
- // Copyright (C) 2011-2013 Internet Systems Consortium, Inc. ("ISC")
- //
- // Permission to use, copy, modify, and/or distribute this software for any
- // purpose with or without fee is hereby granted, provided that the above
- // copyright notice and this permission notice appear in all copies.
- //
- // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- // PERFORMANCE OF THIS SOFTWARE.
- #include <config.h>
- #include <sstream>
- #include <asiolink/io_address.h>
- #include <config/ccsession.h>
- #include <dhcp4/tests/dhcp4_test_utils.h>
- #include <dhcp/dhcp4.h>
- #include <dhcp/iface_mgr.h>
- #include <dhcp/option.h>
- #include <dhcp/option_int.h>
- #include <dhcp/option4_addrlst.h>
- #include <dhcp/option_custom.h>
- #include <dhcp/option_int_array.h>
- #include <dhcp/option_vendor.h>
- #include <dhcp/pkt_filter.h>
- #include <dhcp/pkt_filter_inet.h>
- #include <dhcp/docsis3_option_defs.h>
- #include <dhcp4/dhcp4_srv.h>
- #include <dhcp4/dhcp4_log.h>
- #include <dhcp4/config_parser.h>
- #include <hooks/server_hooks.h>
- #include <dhcpsrv/cfgmgr.h>
- #include <dhcpsrv/lease_mgr.h>
- #include <dhcpsrv/lease_mgr_factory.h>
- #include <dhcpsrv/utils.h>
- #include <gtest/gtest.h>
- #include <hooks/server_hooks.h>
- #include <hooks/hooks_manager.h>
- #include <config/ccsession.h>
- #include <boost/scoped_ptr.hpp>
- #include <fstream>
- #include <iostream>
- #include <arpa/inet.h>
- using namespace std;
- using namespace isc;
- using namespace isc::dhcp;
- using namespace isc::data;
- using namespace isc::asiolink;
- using namespace isc::hooks;
- using namespace isc::dhcp::test;
- namespace {
- /// dummy server-id file location
- const char* SRVID_FILE = "server-id-test.txt";
- // Sanity check. Verifies that both Dhcpv4Srv and its derived
- // class NakedDhcpv4Srv can be instantiated and destroyed.
- TEST_F(Dhcpv4SrvTest, basic) {
- // Check that the base class can be instantiated
- boost::scoped_ptr<Dhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new Dhcpv4Srv(DHCP4_SERVER_PORT + 10000, "type=memfile",
- false, false)));
- srv.reset();
- // We have to close open sockets because further in this test we will
- // call the Dhcpv4Srv constructor again. This constructor will try to
- // set the appropriate packet filter class for IfaceMgr. This requires
- // that all sockets are closed.
- IfaceMgr::instance().closeSockets();
- // Check that the derived class can be instantiated
- boost::scoped_ptr<NakedDhcpv4Srv> naked_srv;
- ASSERT_NO_THROW(
- naked_srv.reset(new NakedDhcpv4Srv(DHCP4_SERVER_PORT + 10000)));
- EXPECT_TRUE(naked_srv->getServerID());
- // Close sockets again for the next test.
- IfaceMgr::instance().closeSockets();
- ASSERT_NO_THROW(naked_srv.reset(new NakedDhcpv4Srv(0)));
- EXPECT_TRUE(naked_srv->getServerID());
- }
- // This test verifies that exception is not thrown when an error occurs during
- // opening sockets. This test forces an error by adding a fictious interface
- // to the IfaceMgr. An attempt to open socket on this interface must always
- // fail. The DHCPv4 installs the error handler function to prevent exceptions
- // being thrown from the openSockets4 function.
- // @todo The server tests for socket should be extended but currently the
- // ability to unit test the sockets code is somewhat limited.
- TEST_F(Dhcpv4SrvTest, openActiveSockets) {
- ASSERT_NO_THROW(CfgMgr::instance().activateAllIfaces());
- Iface iface("bogusiface", 255);
- iface.flag_loopback_ = false;
- iface.flag_up_ = true;
- iface.flag_running_ = true;
- iface.inactive4_ = false;
- iface.addAddress(IOAddress("192.0.0.0"));
- IfaceMgr::instance().addInterface(iface);
- ASSERT_NO_THROW(Dhcpv4Srv::openActiveSockets(DHCP4_SERVER_PORT, false));
- }
- // This test verifies that the destination address of the response
- // message is set to giaddr, when giaddr is set to non-zero address
- // in the received message.
- TEST_F(Dhcpv4SrvTest, adjustRemoteAddressRelay) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv(new NakedDhcpv4Srv(0));
- // Create the instance of the incoming packet.
- boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
- // Set the giaddr to non-zero address as if it was relayed.
- req->setGiaddr(IOAddress("192.0.2.1"));
- // Set ciaddr to zero. This simulates the client which applies
- // for the new lease.
- req->setCiaddr(IOAddress("0.0.0.0"));
- // Clear broadcast flag.
- req->setFlags(0x0000);
- // Create a response packet. Assume that the new lease have
- // been created and new address allocated. This address is
- // stored in yiaddr field.
- boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
- resp->setYiaddr(IOAddress("192.0.2.100"));
- // Clear the remote address.
- resp->setRemoteAddr(IOAddress("0.0.0.0"));
- // This function never throws.
- ASSERT_NO_THROW(srv->adjustRemoteAddr(req, resp));
- // Now the destination address should be relay's address.
- EXPECT_EQ("192.0.2.1", resp->getRemoteAddr().toText());
- // Let's do another test and set other fields: ciaddr and
- // flags. By doing it, we want to make sure that the relay
- // address will take precedence.
- req->setGiaddr(IOAddress("192.0.2.50"));
- req->setCiaddr(IOAddress("192.0.2.11"));
- req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
- resp->setYiaddr(IOAddress("192.0.2.100"));
- // Clear remote address.
- resp->setRemoteAddr(IOAddress("0.0.0.0"));
- ASSERT_NO_THROW(srv->adjustRemoteAddr(req, resp));
- // Response should be sent back to the relay address.
- EXPECT_EQ("192.0.2.50", resp->getRemoteAddr().toText());
- }
- // This test verifies that the destination address of the response message
- // is set to ciaddr when giaddr is set to zero and the ciaddr is set to
- // non-zero address in the received message. This is the case when the
- // client is in Renew or Rebind state.
- TEST_F(Dhcpv4SrvTest, adjustRemoteAddressRenewRebind) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv(new NakedDhcpv4Srv(0));
- // Create instance of the incoming packet.
- boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
- // Clear giaddr to simulate direct packet.
- req->setGiaddr(IOAddress("0.0.0.0"));
- // Set ciaddr to non-zero address. The response should be sent to this
- // address as the client is in renewing or rebinding state (it is fully
- // configured).
- req->setCiaddr(IOAddress("192.0.2.15"));
- // Let's configure broadcast flag. It should be ignored because
- // we are responding directly to the client having an address
- // and trying to extend his lease. Broadcast flag is only used
- // when new lease is acquired and server must make a decision
- // whether to unicast the response to the acquired address or
- // broadcast it.
- req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
- // Create a response.
- boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
- // Let's extend the lease for the client in such a way that
- // it will actually get different address. The response
- // should not be sent to this address but rather to ciaddr
- // as client still have ciaddr configured.
- resp->setYiaddr(IOAddress("192.0.2.13"));
- // Clear the remote address.
- resp->setRemoteAddr(IOAddress("0.0.0.0"));
- ASSERT_NO_THROW(srv->adjustRemoteAddr(req, resp));
- // Check that server responds to ciaddr
- EXPECT_EQ("192.0.2.15", resp->getRemoteAddr().toText());
- }
- // This test verifies that the destination address of the response message
- // is set correctly when giaddr and ciaddr is zeroed in the received message
- // and the new lease is acquired. The lease address is carried in the
- // response message in the yiaddr field. In this case destination address
- // of the response should be set to yiaddr if server supports direct responses
- // to the client which doesn't have an address yet or broadcast if the server
- // doesn't support direct responses.
- TEST_F(Dhcpv4SrvTest, adjustRemoteAddressSelect) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv(new NakedDhcpv4Srv(0));
- // Create instance of the incoming packet.
- boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
- // Clear giaddr to simulate direct packet.
- req->setGiaddr(IOAddress("0.0.0.0"));
- // Clear client address as it hasn't got any address configured yet.
- req->setCiaddr(IOAddress("0.0.0.0"));
- // Let's clear the broadcast flag.
- req->setFlags(0);
- // Create a response.
- boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
- // Assign some new address for this client.
- resp->setYiaddr(IOAddress("192.0.2.13"));
- // Clear the remote address.
- resp->setRemoteAddr(IOAddress("0.0.0.0"));
- // When running unit tests, the IfaceMgr is using the default Packet
- // Filtering class, PktFilterInet. This class does not support direct
- // responses to clients without address assigned. When giaddr and ciaddr
- // are zero and client has just got new lease, the assigned address is
- // carried in yiaddr. In order to send this address to the client,
- // server must broadcast its response.
- ASSERT_NO_THROW(srv->adjustRemoteAddr(req, resp));
- // Check that the response is sent to broadcast address as the
- // server doesn't have capability to respond directly.
- EXPECT_EQ("255.255.255.255", resp->getRemoteAddr().toText());
- // We also want to test the case when the server has capability to
- // respond directly to the client which is not configured. Server
- // makes decision whether it responds directly or broadcast its
- // response based on the capability reported by IfaceMgr. In order
- // to set this capability we have to provide a dummy Packet Filter
- // class which would report the support for direct responses.
- // This class is called PktFilterTest.
- IfaceMgr::instance().setPacketFilter(PktFilterPtr(new PktFilterTest()));
- // Now we expect that the server will send its response to the
- // address assigned for the client.
- ASSERT_NO_THROW(srv->adjustRemoteAddr(req, resp));
- EXPECT_EQ("192.0.2.13", resp->getRemoteAddr().toText());
- }
- // This test verifies that the destination address of the response message
- // is set to broadcast address when client set broadcast flag in its
- // query. Client sets this flag to indicate that it can't receive direct
- // responses from the server when it doesn't have its interface configured.
- // Server must respect broadcast flag.
- TEST_F(Dhcpv4SrvTest, adjustRemoteAddressBroadcast) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv(new NakedDhcpv4Srv(0));
- // Create instance of the incoming packet.
- boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
- // Clear giaddr to simulate direct packet.
- req->setGiaddr(IOAddress("0.0.0.0"));
- // Clear client address as it hasn't got any address configured yet.
- req->setCiaddr(IOAddress("0.0.0.0"));
- // Let's set the broadcast flag.
- req->setFlags(Pkt4::FLAG_BROADCAST_MASK);
- // Create a response.
- boost::shared_ptr<Pkt4> resp(new Pkt4(DHCPOFFER, 1234));
- // Assign some new address for this client.
- resp->setYiaddr(IOAddress("192.0.2.13"));
- // Clear the remote address.
- resp->setRemoteAddr(IOAddress("0.0.0.0"));
- // When running unit tests, the IfaceMgr is using the default Packet
- // Filtering class, PktFilterInet. This class does not support direct
- // responses to the clients without address assigned. If giaddr and
- // ciaddr are zero and client has just got the new lease, the assigned
- // address is carried in yiaddr. In order to send this address to the
- // client, server must send the response to the broadcast address when
- // direct response is not supported. This conflicts with the purpose
- // of this test which is supposed to verify that responses are sent
- // to broadcast address only, when broadcast flag is set. Therefore,
- // in order to simulate that direct responses are supported we have
- // to replace the default packet filtering class with a dummy class
- // which reports direct response capability.
- IfaceMgr::instance().setPacketFilter(PktFilterPtr(new PktFilterTest()));
- ASSERT_NO_THROW(srv->adjustRemoteAddr(req, resp));
- // Server must repond to broadcast address when client desired that
- // by setting the broadcast flag in its request.
- EXPECT_EQ("255.255.255.255", resp->getRemoteAddr().toText());
- }
- // Verifies that DISCOVER message can be processed correctly,
- // that the OFFER message generated in response is valid and
- // contains necessary options.
- //
- // Note: this test focuses on the packet correctness. There
- // are other tests that verify correctness of the allocation
- // engine. See DiscoverBasic, DiscoverHint, DiscoverNoClientId
- // and DiscoverInvalidHint.
- TEST_F(Dhcpv4SrvTest, processDiscover) {
- testDiscoverRequest(DHCPDISCOVER);
- }
- // Verifies that REQUEST message can be processed correctly,
- // that the OFFER message generated in response is valid and
- // contains necessary options.
- //
- // Note: this test focuses on the packet correctness. There
- // are other tests that verify correctness of the allocation
- // engine. See DiscoverBasic, DiscoverHint, DiscoverNoClientId
- // and DiscoverInvalidHint.
- TEST_F(Dhcpv4SrvTest, processRequest) {
- testDiscoverRequest(DHCPREQUEST);
- }
- TEST_F(Dhcpv4SrvTest, processRelease) {
- NakedDhcpv4Srv srv;
- Pkt4Ptr pkt(new Pkt4(DHCPRELEASE, 1234));
- // Should not throw
- EXPECT_NO_THROW(srv.processRelease(pkt));
- }
- TEST_F(Dhcpv4SrvTest, processDecline) {
- NakedDhcpv4Srv srv;
- Pkt4Ptr pkt(new Pkt4(DHCPDECLINE, 1234));
- // Should not throw
- EXPECT_NO_THROW(srv.processDecline(pkt));
- }
- TEST_F(Dhcpv4SrvTest, processInform) {
- NakedDhcpv4Srv srv;
- Pkt4Ptr pkt(new Pkt4(DHCPINFORM, 1234));
- // Should not throw
- EXPECT_NO_THROW(srv.processInform(pkt));
- // Should return something
- EXPECT_TRUE(srv.processInform(pkt));
- // @todo Implement more reasonable tests before starting
- // work on processSomething() method.
- }
- TEST_F(Dhcpv4SrvTest, serverReceivedPacketName) {
- // Check all possible packet types
- for (int itype = 0; itype < 256; ++itype) {
- uint8_t type = itype;
- switch (type) {
- case DHCPDECLINE:
- EXPECT_STREQ("DECLINE", Dhcpv4Srv::serverReceivedPacketName(type));
- break;
- case DHCPDISCOVER:
- EXPECT_STREQ("DISCOVER", Dhcpv4Srv::serverReceivedPacketName(type));
- break;
- case DHCPINFORM:
- EXPECT_STREQ("INFORM", Dhcpv4Srv::serverReceivedPacketName(type));
- break;
- case DHCPRELEASE:
- EXPECT_STREQ("RELEASE", Dhcpv4Srv::serverReceivedPacketName(type));
- break;
- case DHCPREQUEST:
- EXPECT_STREQ("REQUEST", Dhcpv4Srv::serverReceivedPacketName(type));
- break;
- default:
- EXPECT_STREQ("UNKNOWN", Dhcpv4Srv::serverReceivedPacketName(type));
- }
- }
- }
- // This test verifies that incoming DISCOVER can be handled properly, that an
- // OFFER is generated, that the response has an address and that address
- // really belongs to the configured pool.
- //
- // constructed very simple DISCOVER message with:
- // - client-id option
- //
- // expected returned OFFER message:
- // - copy of client-id
- // - server-id
- // - offered address
- TEST_F(Dhcpv4SrvTest, DiscoverBasic) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
- Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
- dis->setRemoteAddr(IOAddress("192.0.2.1"));
- OptionPtr clientid = generateClientId();
- dis->addOption(clientid);
- // Pass it to the server and get an offer
- Pkt4Ptr offer = srv->processDiscover(dis);
- // Check if we get response at all
- checkResponse(offer, DHCPOFFER, 1234);
- // Check that address was returned from proper range, that its lease
- // lifetime is correct, that T1 and T2 are returned properly
- checkAddressParams(offer, subnet_);
- // Check identifiers
- checkServerId(offer, srv->getServerID());
- checkClientId(offer, clientid);
- }
- // This test verifies that incoming DISCOVER can be handled properly, that an
- // OFFER is generated, that the response has an address and that address
- // really belongs to the configured pool.
- //
- // constructed very simple DISCOVER message with:
- // - client-id option
- // - address set to specific value as hint
- //
- // expected returned OFFER message:
- // - copy of client-id
- // - server-id
- // - offered address
- TEST_F(Dhcpv4SrvTest, DiscoverHint) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
- IOAddress hint("192.0.2.107");
- Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
- dis->setRemoteAddr(IOAddress("192.0.2.1"));
- OptionPtr clientid = generateClientId();
- dis->addOption(clientid);
- dis->setYiaddr(hint);
- // Pass it to the server and get an offer
- Pkt4Ptr offer = srv->processDiscover(dis);
- // Check if we get response at all
- checkResponse(offer, DHCPOFFER, 1234);
- // Check that address was returned from proper range, that its lease
- // lifetime is correct, that T1 and T2 are returned properly
- checkAddressParams(offer, subnet_);
- EXPECT_EQ(offer->getYiaddr().toText(), hint.toText());
- // Check identifiers
- checkServerId(offer, srv->getServerID());
- checkClientId(offer, clientid);
- }
- // This test verifies that incoming DISCOVER can be handled properly, that an
- // OFFER is generated, that the response has an address and that address
- // really belongs to the configured pool.
- //
- // constructed very simple DISCOVER message with:
- // - address set to specific value as hint
- //
- // expected returned OFFER message:
- // - copy of client-id
- // - server-id
- // - offered address
- TEST_F(Dhcpv4SrvTest, DiscoverNoClientId) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
- IOAddress hint("192.0.2.107");
- Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
- dis->setRemoteAddr(IOAddress("192.0.2.1"));
- dis->setYiaddr(hint);
- dis->setHWAddr(generateHWAddr(6));
- // Pass it to the server and get an offer
- Pkt4Ptr offer = srv->processDiscover(dis);
- // Check if we get response at all
- checkResponse(offer, DHCPOFFER, 1234);
- // Check that address was returned from proper range, that its lease
- // lifetime is correct, that T1 and T2 are returned properly
- checkAddressParams(offer, subnet_);
- EXPECT_EQ(offer->getYiaddr().toText(), hint.toText());
- // Check identifiers
- checkServerId(offer, srv->getServerID());
- }
- // This test verifies that incoming DISCOVER can be handled properly, that an
- // OFFER is generated, that the response has an address and that address
- // really belongs to the configured pool.
- //
- // constructed very simple DISCOVER message with:
- // - client-id option
- // - address set to specific value as hint, but that hint is invalid
- //
- // expected returned OFFER message:
- // - copy of client-id
- // - server-id
- // - offered address (!= hint)
- TEST_F(Dhcpv4SrvTest, DiscoverInvalidHint) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
- IOAddress hint("10.1.2.3");
- Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
- dis->setRemoteAddr(IOAddress("192.0.2.107"));
- OptionPtr clientid = generateClientId();
- dis->addOption(clientid);
- dis->setYiaddr(hint);
- // Pass it to the server and get an offer
- Pkt4Ptr offer = srv->processDiscover(dis);
- // Check if we get response at all
- checkResponse(offer, DHCPOFFER, 1234);
- // Check that address was returned from proper range, that its lease
- // lifetime is correct, that T1 and T2 are returned properly
- checkAddressParams(offer, subnet_);
- EXPECT_NE(offer->getYiaddr().toText(), hint.toText());
- // Check identifiers
- checkServerId(offer, srv->getServerID());
- checkClientId(offer, clientid);
- }
- /// @todo: Add a test that client sends hint that is in pool, but currently
- /// being used by a different client.
- // This test checks that the server is offering different addresses to different
- // clients in OFFERs. Please note that OFFER is not a guarantee that such
- // an address will be assigned. Had the pool was very small and contained only
- // 2 addresses, the third client would get the same offer as the first one
- // and this is a correct behavior. It is REQUEST that will fail for the third
- // client. OFFER is basically saying "if you send me a request, you will
- // probably get an address like this" (there are no guarantees).
- TEST_F(Dhcpv4SrvTest, ManyDiscovers) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
- Pkt4Ptr dis1 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
- Pkt4Ptr dis2 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 2345));
- Pkt4Ptr dis3 = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 3456));
- dis1->setRemoteAddr(IOAddress("192.0.2.1"));
- dis2->setRemoteAddr(IOAddress("192.0.2.2"));
- dis3->setRemoteAddr(IOAddress("192.0.2.3"));
- // Different client-id sizes
- OptionPtr clientid1 = generateClientId(4); // length 4
- OptionPtr clientid2 = generateClientId(5); // length 5
- OptionPtr clientid3 = generateClientId(6); // length 6
- dis1->addOption(clientid1);
- dis2->addOption(clientid2);
- dis3->addOption(clientid3);
- // Pass it to the server and get an offer
- Pkt4Ptr offer1 = srv->processDiscover(dis1);
- Pkt4Ptr offer2 = srv->processDiscover(dis2);
- Pkt4Ptr offer3 = srv->processDiscover(dis3);
- // Check if we get response at all
- checkResponse(offer1, DHCPOFFER, 1234);
- checkResponse(offer2, DHCPOFFER, 2345);
- checkResponse(offer3, DHCPOFFER, 3456);
- IOAddress addr1 = offer1->getYiaddr();
- IOAddress addr2 = offer2->getYiaddr();
- IOAddress addr3 = offer3->getYiaddr();
- // Check that the assigned address is indeed from the configured pool
- checkAddressParams(offer1, subnet_);
- checkAddressParams(offer2, subnet_);
- checkAddressParams(offer3, subnet_);
- // Check server-ids
- checkServerId(offer1, srv->getServerID());
- checkServerId(offer2, srv->getServerID());
- checkServerId(offer3, srv->getServerID());
- checkClientId(offer1, clientid1);
- checkClientId(offer2, clientid2);
- checkClientId(offer3, clientid3);
- // Finally check that the addresses offered are different
- EXPECT_NE(addr1.toText(), addr2.toText());
- EXPECT_NE(addr2.toText(), addr3.toText());
- EXPECT_NE(addr3.toText(), addr1.toText());
- cout << "Offered address to client1=" << addr1.toText() << endl;
- cout << "Offered address to client2=" << addr2.toText() << endl;
- cout << "Offered address to client3=" << addr3.toText() << endl;
- }
- // This test verifies that incoming REQUEST can be handled properly, that an
- // ACK is generated, that the response has an address and that address
- // really belongs to the configured pool.
- //
- // constructed a single REQUEST message with:
- // - client-id option
- // - hwaddr information
- // - requested address (that the client received in DISCOVER/OFFER exchange)
- //
- // expected returned ACK message:
- // - copy of client-id
- // - server-id
- // - assigned address
- //
- // Test verifies that the lease is actually in the database.
- TEST_F(Dhcpv4SrvTest, RequestBasic) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
- IOAddress hint("192.0.2.107");
- Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
- req->setRemoteAddr(IOAddress("192.0.2.1"));
- OptionPtr clientid = generateClientId();
- req->addOption(clientid);
- req->setYiaddr(hint);
- // Pass it to the server and get an advertise
- Pkt4Ptr ack = srv->processRequest(req);
- // Check if we get response at all
- checkResponse(ack, DHCPACK, 1234);
- EXPECT_EQ(hint.toText(), ack->getYiaddr().toText());
- // Check that address was returned from proper range, that its lease
- // lifetime is correct, that T1 and T2 are returned properly
- checkAddressParams(ack, subnet_);
- // Check identifiers
- checkServerId(ack, srv->getServerID());
- checkClientId(ack, clientid);
- // Check that the lease is really in the database
- Lease4Ptr l = checkLease(ack, clientid, req->getHWAddr(), hint);
- ASSERT_TRUE(l);
- LeaseMgrFactory::instance().deleteLease(l->addr_);
- }
- // This test verifies that incoming REQUEST can be handled properly, that an
- // ACK is generated, that the response has an address and that address
- // really belongs to the configured pool.
- //
- // constructed 3 REQUEST messages with:
- // - client-id option (differs between messages)
- // - hwaddr information (differs between messages)
- //
- // expected returned ACK message:
- // - copy of client-id
- // - server-id
- // - assigned address (different for each client)
- TEST_F(Dhcpv4SrvTest, ManyRequests) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
- const IOAddress req_addr1("192.0.2.105");
- const IOAddress req_addr2("192.0.2.101");
- const IOAddress req_addr3("192.0.2.109");
- const IOAddress relay("192.0.2.1");
- Pkt4Ptr req1 = Pkt4Ptr(new Pkt4(DHCPOFFER, 1234));
- Pkt4Ptr req2 = Pkt4Ptr(new Pkt4(DHCPOFFER, 2345));
- Pkt4Ptr req3 = Pkt4Ptr(new Pkt4(DHCPOFFER, 3456));
- req1->setRemoteAddr(relay);
- req2->setRemoteAddr(relay);
- req3->setRemoteAddr(relay);
- req1->setYiaddr(req_addr1);
- req2->setYiaddr(req_addr2);
- req3->setYiaddr(req_addr3);
- req1->setHWAddr(generateHWAddr(6));
- req2->setHWAddr(generateHWAddr(7));
- req3->setHWAddr(generateHWAddr(8));
- // Different client-id sizes
- OptionPtr clientid1 = generateClientId(4); // length 4
- OptionPtr clientid2 = generateClientId(5); // length 5
- OptionPtr clientid3 = generateClientId(6); // length 6
- req1->addOption(clientid1);
- req2->addOption(clientid2);
- req3->addOption(clientid3);
- // Pass it to the server and get an advertise
- Pkt4Ptr ack1 = srv->processRequest(req1);
- Pkt4Ptr ack2 = srv->processRequest(req2);
- Pkt4Ptr ack3 = srv->processRequest(req3);
- // Check if we get response at all
- checkResponse(ack1, DHCPACK, 1234);
- checkResponse(ack2, DHCPACK, 2345);
- checkResponse(ack3, DHCPACK, 3456);
- IOAddress addr1 = ack1->getYiaddr();
- IOAddress addr2 = ack2->getYiaddr();
- IOAddress addr3 = ack3->getYiaddr();
- // Check that every client received the address it requested
- EXPECT_EQ(req_addr1.toText(), addr1.toText());
- EXPECT_EQ(req_addr2.toText(), addr2.toText());
- EXPECT_EQ(req_addr3.toText(), addr3.toText());
- // Check that the assigned address is indeed from the configured pool
- checkAddressParams(ack1, subnet_);
- checkAddressParams(ack2, subnet_);
- checkAddressParams(ack3, subnet_);
- // Check DUIDs
- checkServerId(ack1, srv->getServerID());
- checkServerId(ack2, srv->getServerID());
- checkServerId(ack3, srv->getServerID());
- checkClientId(ack1, clientid1);
- checkClientId(ack2, clientid2);
- checkClientId(ack3, clientid3);
- // Check that leases are in the database
- Lease4Ptr l = checkLease(ack1, clientid1, req1->getHWAddr(), addr1);
- EXPECT_TRUE(l);
- l = checkLease(ack2, clientid2, req2->getHWAddr(), addr2);
- l = checkLease(ack3, clientid3, req3->getHWAddr(), addr3);
- // Finally check that the addresses offered are different
- EXPECT_NE(addr1.toText(), addr2.toText());
- EXPECT_NE(addr2.toText(), addr3.toText());
- EXPECT_NE(addr3.toText(), addr1.toText());
- cout << "Offered address to client1=" << addr1.toText() << endl;
- cout << "Offered address to client2=" << addr2.toText() << endl;
- cout << "Offered address to client3=" << addr3.toText() << endl;
- }
- // This test verifies that incoming (positive) REQUEST/Renewing can be handled properly, that a
- // REPLY is generated, that the response has an address and that address
- // really belongs to the configured pool and that lease is actually renewed.
- //
- // expected:
- // - returned REPLY message has copy of client-id
- // - returned REPLY message has server-id
- // - returned REPLY message has IA that includes IAADDR
- // - lease is actually renewed in LeaseMgr
- TEST_F(Dhcpv4SrvTest, RenewBasic) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
- const IOAddress addr("192.0.2.106");
- const uint32_t temp_t1 = 50;
- const uint32_t temp_t2 = 75;
- const uint32_t temp_valid = 100;
- const time_t temp_timestamp = time(NULL) - 10;
- // Generate client-id also sets client_id_ member
- OptionPtr clientid = generateClientId();
- // Check that the address we are about to use is indeed in pool
- ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
- // let's create a lease and put it in the LeaseMgr
- uint8_t hwaddr2[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
- Lease4Ptr used(new Lease4(IOAddress("192.0.2.106"), hwaddr2, sizeof(hwaddr2),
- &client_id_->getDuid()[0], client_id_->getDuid().size(),
- temp_valid, temp_t1, temp_t2, temp_timestamp,
- subnet_->getID()));
- ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
- // Check that the lease is really in the database
- Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
- ASSERT_TRUE(l);
- // Check that T1, T2, preferred, valid and cltt really set.
- // Constructed lease looks as if it was assigned 10 seconds ago
- // EXPECT_EQ(l->t1_, temp_t1);
- // EXPECT_EQ(l->t2_, temp_t2);
- EXPECT_EQ(l->valid_lft_, temp_valid);
- EXPECT_EQ(l->cltt_, temp_timestamp);
- // Let's create a RENEW
- Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
- req->setRemoteAddr(IOAddress(addr));
- req->setYiaddr(addr);
- req->setCiaddr(addr); // client's address
- req->addOption(clientid);
- req->addOption(srv->getServerID());
- // Pass it to the server and hope for a REPLY
- Pkt4Ptr ack = srv->processRequest(req);
- // Check if we get response at all
- checkResponse(ack, DHCPACK, 1234);
- EXPECT_EQ(addr.toText(), ack->getYiaddr().toText());
- // Check that address was returned from proper range, that its lease
- // lifetime is correct, that T1 and T2 are returned properly
- checkAddressParams(ack, subnet_);
- // Check identifiers
- checkServerId(ack, srv->getServerID());
- checkClientId(ack, clientid);
- // Check that the lease is really in the database
- l = checkLease(ack, clientid, req->getHWAddr(), addr);
- ASSERT_TRUE(l);
- // Check that T1, T2, preferred, valid and cltt were really updated
- EXPECT_EQ(l->t1_, subnet_->getT1());
- EXPECT_EQ(l->t2_, subnet_->getT2());
- EXPECT_EQ(l->valid_lft_, subnet_->getValid());
- // Checking for CLTT is a bit tricky if we want to avoid off by 1 errors
- int32_t cltt = static_cast<int32_t>(l->cltt_);
- int32_t expected = static_cast<int32_t>(time(NULL));
- // Equality or difference by 1 between cltt and expected is ok.
- EXPECT_GE(1, abs(cltt - expected));
- EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
- }
- // @todo: Implement tests for rejecting renewals
- // This test verifies if the sanityCheck() really checks options presence.
- TEST_F(Dhcpv4SrvTest, sanityCheck) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
- Pkt4Ptr pkt = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
- pkt->setHWAddr(generateHWAddr(6));
- // Server-id is optional for information-request, so
- EXPECT_NO_THROW(srv->sanityCheck(pkt, Dhcpv4Srv::OPTIONAL));
- // Empty packet, no server-id
- EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv4Srv::MANDATORY), RFCViolation);
- pkt->addOption(srv->getServerID());
- // Server-id is mandatory and present = no exception
- EXPECT_NO_THROW(srv->sanityCheck(pkt, Dhcpv4Srv::MANDATORY));
- // Server-id is forbidden, but present => exception
- EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv4Srv::FORBIDDEN),
- RFCViolation);
- // There's no client-id and no HWADDR. Server needs something to
- // identify the client
- pkt->setHWAddr(generateHWAddr(0));
- EXPECT_THROW(srv->sanityCheck(pkt, Dhcpv4Srv::MANDATORY), RFCViolation);
- }
- // This test verifies that incoming (positive) RELEASE can be handled properly.
- // As there is no REPLY in DHCPv4, the only thing to verify here is that
- // the lease is indeed removed from the database.
- TEST_F(Dhcpv4SrvTest, ReleaseBasic) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
- const IOAddress addr("192.0.2.106");
- const uint32_t temp_t1 = 50;
- const uint32_t temp_t2 = 75;
- const uint32_t temp_valid = 100;
- const time_t temp_timestamp = time(NULL) - 10;
- // Generate client-id also duid_
- OptionPtr clientid = generateClientId();
- // Check that the address we are about to use is indeed in pool
- ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
- // Let's create a lease and put it in the LeaseMgr
- uint8_t mac_addr[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
- HWAddrPtr hw(new HWAddr(mac_addr, sizeof(mac_addr), HTYPE_ETHER));
- Lease4Ptr used(new Lease4(addr, mac_addr, sizeof(mac_addr),
- &client_id_->getDuid()[0], client_id_->getDuid().size(),
- temp_valid, temp_t1, temp_t2, temp_timestamp,
- subnet_->getID()));
- ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
- // Check that the lease is really in the database
- Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
- ASSERT_TRUE(l);
- // Let's create a RELEASE
- // Generate client-id also duid_
- Pkt4Ptr rel = Pkt4Ptr(new Pkt4(DHCPRELEASE, 1234));
- rel->setRemoteAddr(addr);
- rel->setCiaddr(addr);
- rel->addOption(clientid);
- rel->addOption(srv->getServerID());
- rel->setHWAddr(hw);
- // Pass it to the server and hope for a REPLY
- // Note: this is no response to RELEASE in DHCPv4
- EXPECT_NO_THROW(srv->processRelease(rel));
- // The lease should be gone from LeaseMgr
- l = LeaseMgrFactory::instance().getLease4(addr);
- EXPECT_FALSE(l);
- // Try to get the lease by hardware address
- Lease4Collection leases = LeaseMgrFactory::instance().getLease4(hw->hwaddr_);
- EXPECT_EQ(leases.size(), 0);
- // Try to get it by hw/subnet_id combination
- l = LeaseMgrFactory::instance().getLease4(hw->hwaddr_, subnet_->getID());
- EXPECT_FALSE(l);
- // Try by client-id
- leases = LeaseMgrFactory::instance().getLease4(*client_id_);
- EXPECT_EQ(leases.size(), 0);
- // Try by client-id/subnet-id
- l = LeaseMgrFactory::instance().getLease4(*client_id_, subnet_->getID());
- EXPECT_FALSE(l);
- // Ok, the lease is *really* not there.
- }
- // This test verifies that incoming (invalid) RELEASE can be handled properly.
- //
- // This test checks 3 scenarios:
- // 1. there is no such lease at all
- // 2. there is such a lease, but it is assigned to a different IAID
- // 3. there is such a lease, but it belongs to a different client
- TEST_F(Dhcpv4SrvTest, ReleaseReject) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
- const IOAddress addr("192.0.2.106");
- const uint32_t t1 = 50;
- const uint32_t t2 = 75;
- const uint32_t valid = 100;
- const time_t timestamp = time(NULL) - 10;
- // Let's create a lease and put it in the LeaseMgr
- uint8_t bogus_mac_addr[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
- HWAddrPtr bogus_hw(new HWAddr(bogus_mac_addr, sizeof(bogus_mac_addr), HTYPE_ETHER));
- OptionPtr bogus_clientid = generateClientId(7); // different length
- // Generate client-id also duid_
- OptionPtr clientid = generateClientId();
- // Check that the address we are about to use is indeed in pool
- ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
- // Let's create a RELEASE
- // Generate client-id also duid_
- Pkt4Ptr rel = Pkt4Ptr(new Pkt4(DHCPRELEASE, 1234));
- rel->setRemoteAddr(addr);
- rel->setCiaddr(addr);
- rel->addOption(clientid);
- rel->addOption(srv->getServerID());
- rel->setHWAddr(bogus_hw);
- // Case 1: No lease known to server
- SCOPED_TRACE("CASE 1: Lease is not known to the server");
- // There is nothing to check here. The lease is not there and server does
- // not send anything back. This case is enumerated here just for keeping
- // parity with similar test in DHCPv6.
- EXPECT_NO_THROW(srv->processRelease(rel));
- // CASE 2: Lease is known and belongs to this client, but to a different hardware
- SCOPED_TRACE("CASE 2: Lease is known and belongs to this client, but uses different HW addr");
- // Let's create a lease and put it in the LeaseMgr
- uint8_t mac_addr[] = { 0, 0x1, 0x2, 0x3, 0x4, 0x5};
- HWAddrPtr hw(new HWAddr(mac_addr, sizeof(mac_addr), HTYPE_ETHER));
- Lease4Ptr used(new Lease4(addr, mac_addr, sizeof(mac_addr),
- &client_id_->getDuid()[0], client_id_->getDuid().size(),
- valid, t1, t2, timestamp, subnet_->getID()));
- ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
- // Check that the lease is really in the database
- Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
- ASSERT_TRUE(l);
- rel->setHWAddr(bogus_hw);
- EXPECT_NO_THROW(srv->processRelease(rel));
- // Check that the lease was not removed (due to hardware address mis-match)
- l = LeaseMgrFactory::instance().getLease4(addr);
- ASSERT_TRUE(l);
- // CASE 3: Lease belongs to a client with different client-id
- SCOPED_TRACE("CASE 3: Lease belongs to a client with different client-id");
- rel->setHWAddr(hw); // proper HW address this time
- rel->delOption(DHO_DHCP_CLIENT_IDENTIFIER);
- rel->addOption(bogus_clientid); // but invalid client-id
- OptionPtr x = rel->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
- EXPECT_NO_THROW(srv->processRelease(rel));
- // Check that the lease is still there
- l = LeaseMgrFactory::instance().getLease4(addr);
- ASSERT_TRUE(l);
- // Final sanity check. Verify that with valid hw and client-id release is successful
- rel->delOption(DHO_DHCP_CLIENT_IDENTIFIER);
- rel->addOption(clientid);
- // It should work this time
- EXPECT_NO_THROW(srv->processRelease(rel));
- // Check that the lease is not there
- l = LeaseMgrFactory::instance().getLease4(addr);
- EXPECT_FALSE(l);
- }
- // This test verifies if the server-id disk operations (read, write) are
- // working properly.
- TEST_F(Dhcpv4SrvTest, ServerID) {
- NakedDhcpv4Srv srv(0);
- string srvid_text = "192.0.2.100";
- IOAddress srvid(srvid_text);
- fstream file1(SRVID_FILE, ios::out | ios::trunc);
- file1 << srvid_text;
- file1.close();
- // Test reading from a file
- EXPECT_TRUE(srv.loadServerID(SRVID_FILE));
- ASSERT_TRUE(srv.getServerID());
- EXPECT_EQ(srvid_text, srv.srvidToString(srv.getServerID()));
- // Now test writing to a file
- EXPECT_EQ(0, unlink(SRVID_FILE));
- EXPECT_NO_THROW(srv.writeServerID(SRVID_FILE));
- fstream file2(SRVID_FILE, ios::in);
- ASSERT_TRUE(file2.good());
- string text;
- file2 >> text;
- file2.close();
- EXPECT_EQ(srvid_text, text);
- }
- // Checks if received relay agent info option is echoed back to the client
- TEST_F(Dhcpv4SrvTest, relayAgentInfoEcho) {
- NakedDhcpv4Srv srv(0);
- // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
- // added option 82 (relay agent info) with 3 suboptions. The server
- // is supposed to echo it back in its response.
- Pkt4Ptr dis;
- ASSERT_NO_THROW(dis = captureRelayedDiscover());
- // Simulate that we have received that traffic
- srv.fakeReceive(dis);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered buffer4_receive callback.
- srv.run();
- // Check that the server did send a reposonse
- ASSERT_EQ(1, srv.fake_sent_.size());
- // Make sure that we received a response
- Pkt4Ptr offer = srv.fake_sent_.front();
- ASSERT_TRUE(offer);
- // Get Relay Agent Info from query...
- OptionPtr rai_query = dis->getOption(DHO_DHCP_AGENT_OPTIONS);
- ASSERT_TRUE(rai_query);
- // Get Relay Agent Info from response...
- OptionPtr rai_response = offer->getOption(DHO_DHCP_AGENT_OPTIONS);
- ASSERT_TRUE(rai_response);
- EXPECT_TRUE(rai_response->equal(rai_query));
- }
- /// @todo move vendor options tests to a separate file.
- /// @todo Add more extensive vendor options tests, including multiple
- /// vendor options
- // Checks if vendor options are parsed correctly and requested vendor options
- // are echoed back.
- TEST_F(Dhcpv4SrvTest, vendorOptionsDocsis) {
- NakedDhcpv4Srv srv(0);
- string config = "{ \"interfaces\": [ \"*\" ],"
- "\"rebind-timer\": 2000, "
- "\"renew-timer\": 1000, "
- " \"option-data\": [ {"
- " \"name\": \"tftp-servers\","
- " \"space\": \"vendor-4491\","
- " \"code\": 2,"
- " \"data\": \"10.253.175.16\","
- " \"csv-format\": True"
- " }],"
- "\"subnet4\": [ { "
- " \"pool\": [ \"10.254.226.0/25\" ],"
- " \"subnet\": \"10.254.226.0/24\", "
- " \"rebind-timer\": 2000, "
- " \"renew-timer\": 1000, "
- " \"valid-lifetime\": 4000,"
- " \"interface\": \"" + valid_iface_ + "\" "
- " } ],"
- "\"valid-lifetime\": 4000 }";
- ElementPtr json = Element::fromJSON(config);
- ConstElementPtr status;
- // Configure the server and make sure the config is accepted
- EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
- ASSERT_TRUE(status);
- comment_ = config::parseAnswer(rcode_, status);
- ASSERT_EQ(0, rcode_);
- // Let's create a relayed DISCOVER. This particular relayed DISCOVER has
- // added option 82 (relay agent info) with 3 suboptions. The server
- // is supposed to echo it back in its response.
- Pkt4Ptr dis;
- ASSERT_NO_THROW(dis = captureRelayedDiscover());
- // Simulate that we have received that traffic
- srv.fakeReceive(dis);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered buffer4_receive callback.
- srv.run();
- // Check that the server did send a reposonse
- ASSERT_EQ(1, srv.fake_sent_.size());
- // Make sure that we received a response
- Pkt4Ptr offer = srv.fake_sent_.front();
- ASSERT_TRUE(offer);
- // Get Relay Agent Info from query...
- OptionPtr vendor_opt_response = offer->getOption(DHO_VIVSO_SUBOPTIONS);
- ASSERT_TRUE(vendor_opt_response);
- // Check if it's of a correct type
- boost::shared_ptr<OptionVendor> vendor_opt =
- boost::dynamic_pointer_cast<OptionVendor>(vendor_opt_response);
- ASSERT_TRUE(vendor_opt);
- // Get Relay Agent Info from response...
- OptionPtr tftp_servers_generic = vendor_opt->getOption(DOCSIS3_V4_TFTP_SERVERS);
- ASSERT_TRUE(tftp_servers_generic);
- Option4AddrLstPtr tftp_servers =
- boost::dynamic_pointer_cast<Option4AddrLst>(tftp_servers_generic);
- ASSERT_TRUE(tftp_servers);
- Option4AddrLst::AddressContainer addrs = tftp_servers->getAddresses();
- ASSERT_EQ(1, addrs.size());
- EXPECT_EQ("10.253.175.16", addrs[0].toText());
- }
- /// @todo Implement tests for subnetSelect See tests in dhcp6_srv_unittest.cc:
- /// selectSubnetAddr, selectSubnetIface, selectSubnetRelayLinkaddr,
- /// selectSubnetRelayInterfaceId. Note that the concept of interface-id is not
- /// present in the DHCPv4, so not everything is applicable directly.
- /// See ticket #3057
- // Checks if hooks are registered properly.
- TEST_F(Dhcpv4SrvTest, Hooks) {
- NakedDhcpv4Srv srv(0);
- // check if appropriate hooks are registered
- int hook_index_buffer4_receive = -1;
- int hook_index_pkt4_receive = -1;
- int hook_index_select_subnet = -1;
- int hook_index_lease4_release = -1;
- int hook_index_pkt4_send = -1;
- int hook_index_buffer4_send = -1;
- // check if appropriate indexes are set
- EXPECT_NO_THROW(hook_index_buffer4_receive = ServerHooks::getServerHooks()
- .getIndex("buffer4_receive"));
- EXPECT_NO_THROW(hook_index_pkt4_receive = ServerHooks::getServerHooks()
- .getIndex("pkt4_receive"));
- EXPECT_NO_THROW(hook_index_select_subnet = ServerHooks::getServerHooks()
- .getIndex("subnet4_select"));
- EXPECT_NO_THROW(hook_index_lease4_release = ServerHooks::getServerHooks()
- .getIndex("lease4_release"));
- EXPECT_NO_THROW(hook_index_pkt4_send = ServerHooks::getServerHooks()
- .getIndex("pkt4_send"));
- EXPECT_NO_THROW(hook_index_buffer4_send = ServerHooks::getServerHooks()
- .getIndex("buffer4_send"));
- EXPECT_TRUE(hook_index_buffer4_receive > 0);
- EXPECT_TRUE(hook_index_pkt4_receive > 0);
- EXPECT_TRUE(hook_index_select_subnet > 0);
- EXPECT_TRUE(hook_index_lease4_release > 0);
- EXPECT_TRUE(hook_index_pkt4_send > 0);
- EXPECT_TRUE(hook_index_buffer4_send > 0);
- }
- // This test verifies that the following option structure can be parsed:
- // - option (option space 'foobar')
- // - sub option (option space 'foo')
- // - sub option (option space 'bar')
- // @todo Add more thorough unit tests for unpackOptions.
- TEST_F(Dhcpv4SrvTest, unpackOptions) {
- // Create option definition for each level of encapsulation. Each option
- // definition is for the option code 1. Options may have the same
- // option code because they belong to different option spaces.
- // Top level option encapsulates options which belong to 'space-foo'.
- OptionDefinitionPtr opt_def(new OptionDefinition("option-foobar", 1, "uint32",
- "space-foo"));\
- // Middle option encapsulates options which belong to 'space-bar'
- OptionDefinitionPtr opt_def2(new OptionDefinition("option-foo", 1, "uint16",
- "space-bar"));
- // Low level option doesn't encapsulate any option space.
- OptionDefinitionPtr opt_def3(new OptionDefinition("option-bar", 1,
- "uint8"));
- // Add option definitions to the Configuration Manager. Each goes under
- // different option space.
- CfgMgr& cfgmgr = CfgMgr::instance();
- ASSERT_NO_THROW(cfgmgr.addOptionDef(opt_def, "space-foobar"));
- ASSERT_NO_THROW(cfgmgr.addOptionDef(opt_def2, "space-foo"));
- ASSERT_NO_THROW(cfgmgr.addOptionDef(opt_def3, "space-bar"));
- // Create the buffer holding the structure of options.
- const char raw_data[] = {
- // First option starts here.
- 0x01, // option code = 1
- 0x0B, // option length = 11
- 0x00, 0x01, 0x02, 0x03, // This option carries uint32 value
- // Sub option starts here.
- 0x01, // option code = 1
- 0x05, // option length = 5
- 0x01, 0x02, // this option carries uint16 value
- // Last option starts here.
- 0x01, // option code = 1
- 0x01, // option length = 1
- 0x00 // This option carries a single uint8
- // value and has no sub options.
- };
- OptionBuffer buf(raw_data, raw_data + sizeof(raw_data));
- // Parse options.
- NakedDhcpv4Srv srv(0);
- OptionCollection options;
- ASSERT_NO_THROW(srv.unpackOptions(buf, "space-foobar", options));
- // There should be one top level option.
- ASSERT_EQ(1, options.size());
- boost::shared_ptr<OptionInt<uint32_t> > option_foobar =
- boost::dynamic_pointer_cast<OptionInt<uint32_t> >(options.begin()->
- second);
- ASSERT_TRUE(option_foobar);
- EXPECT_EQ(1, option_foobar->getType());
- EXPECT_EQ(0x00010203, option_foobar->getValue());
- // There should be a middle level option held in option_foobar.
- boost::shared_ptr<OptionInt<uint16_t> > option_foo =
- boost::dynamic_pointer_cast<OptionInt<uint16_t> >(option_foobar->
- getOption(1));
- ASSERT_TRUE(option_foo);
- EXPECT_EQ(1, option_foo->getType());
- EXPECT_EQ(0x0102, option_foo->getValue());
- // Finally, there should be a low level option under option_foo.
- boost::shared_ptr<OptionInt<uint8_t> > option_bar =
- boost::dynamic_pointer_cast<OptionInt<uint8_t> >(option_foo->getOption(1));
- ASSERT_TRUE(option_bar);
- EXPECT_EQ(1, option_bar->getType());
- EXPECT_EQ(0x0, option_bar->getValue());
- }
- // Checks whether the server uses default (0.0.0.0) siaddr value, unless
- // explicitly specified
- TEST_F(Dhcpv4SrvTest, siaddrDefault) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
- IOAddress hint("192.0.2.107");
- Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
- dis->setRemoteAddr(IOAddress("192.0.2.1"));
- OptionPtr clientid = generateClientId();
- dis->addOption(clientid);
- dis->setYiaddr(hint);
- // Pass it to the server and get an offer
- Pkt4Ptr offer = srv->processDiscover(dis);
- ASSERT_TRUE(offer);
- // Check if we get response at all
- checkResponse(offer, DHCPOFFER, 1234);
- // Verify that it is 0.0.0.0
- EXPECT_EQ("0.0.0.0", offer->getSiaddr().toText());
- }
- // Checks whether the server uses specified siaddr value
- TEST_F(Dhcpv4SrvTest, siaddr) {
- boost::scoped_ptr<NakedDhcpv4Srv> srv;
- ASSERT_NO_THROW(srv.reset(new NakedDhcpv4Srv(0)));
- subnet_->setSiaddr(IOAddress("192.0.2.123"));
- Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
- dis->setRemoteAddr(IOAddress("192.0.2.1"));
- OptionPtr clientid = generateClientId();
- dis->addOption(clientid);
- // Pass it to the server and get an offer
- Pkt4Ptr offer = srv->processDiscover(dis);
- ASSERT_TRUE(offer);
- // Check if we get response at all
- checkResponse(offer, DHCPOFFER, 1234);
- // Verify that its value is proper
- EXPECT_EQ("192.0.2.123", offer->getSiaddr().toText());
- }
- // Checks if the next-server defined as global value is overridden by subnet
- // specific value and returned in server messages. There's also similar test for
- // checking parser only configuration, see Dhcp4ParserTest.nextServerOverride in
- // config_parser_unittest.cc.
- TEST_F(Dhcpv4SrvTest, nextServerOverride) {
- NakedDhcpv4Srv srv(0);
- ConstElementPtr status;
- string config = "{ \"interfaces\": [ \"*\" ],"
- "\"rebind-timer\": 2000, "
- "\"renew-timer\": 1000, "
- "\"next-server\": \"192.0.0.1\", "
- "\"subnet4\": [ { "
- " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
- " \"next-server\": \"1.2.3.4\", "
- " \"subnet\": \"192.0.2.0/24\" } ],"
- "\"valid-lifetime\": 4000 }";
- ElementPtr json = Element::fromJSON(config);
- EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
- // check if returned status is OK
- ASSERT_TRUE(status);
- comment_ = config::parseAnswer(rcode_, status);
- ASSERT_EQ(0, rcode_);
- Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
- dis->setRemoteAddr(IOAddress("192.0.2.1"));
- OptionPtr clientid = generateClientId();
- dis->addOption(clientid);
- // Pass it to the server and get an offer
- Pkt4Ptr offer = srv.processDiscover(dis);
- ASSERT_TRUE(offer);
- EXPECT_EQ(DHCPOFFER, offer->getType());
- EXPECT_EQ("1.2.3.4", offer->getSiaddr().toText());
- }
- // Checks if the next-server defined as global value is used in responses
- // when there is no specific value defined in subnet and returned to the client
- // properly. There's also similar test for checking parser only configuration,
- // see Dhcp4ParserTest.nextServerGlobal in config_parser_unittest.cc.
- TEST_F(Dhcpv4SrvTest, nextServerGlobal) {
- NakedDhcpv4Srv srv(0);
- ConstElementPtr status;
- string config = "{ \"interfaces\": [ \"*\" ],"
- "\"rebind-timer\": 2000, "
- "\"renew-timer\": 1000, "
- "\"next-server\": \"192.0.0.1\", "
- "\"subnet4\": [ { "
- " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
- " \"subnet\": \"192.0.2.0/24\" } ],"
- "\"valid-lifetime\": 4000 }";
- ElementPtr json = Element::fromJSON(config);
- EXPECT_NO_THROW(status = configureDhcp4Server(srv, json));
- // check if returned status is OK
- ASSERT_TRUE(status);
- comment_ = config::parseAnswer(rcode_, status);
- ASSERT_EQ(0, rcode_);
- Pkt4Ptr dis = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
- dis->setRemoteAddr(IOAddress("192.0.2.1"));
- OptionPtr clientid = generateClientId();
- dis->addOption(clientid);
- // Pass it to the server and get an offer
- Pkt4Ptr offer = srv.processDiscover(dis);
- ASSERT_TRUE(offer);
- EXPECT_EQ(DHCPOFFER, offer->getType());
- EXPECT_EQ("192.0.0.1", offer->getSiaddr().toText());
- }
- // a dummy MAC address
- const uint8_t dummyMacAddr[] = {0, 1, 2, 3, 4, 5};
- // A dummy MAC address, padded with 0s
- const uint8_t dummyChaddr[16] = {0, 1, 2, 3, 4, 5, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0 };
- // Let's use some creative test content here (128 chars + \0)
- const uint8_t dummyFile[] = "Lorem ipsum dolor sit amet, consectetur "
- "adipiscing elit. Proin mollis placerat metus, at "
- "lacinia orci ornare vitae. Mauris amet.";
- // Yet another type of test content (64 chars + \0)
- const uint8_t dummySname[] = "Lorem ipsum dolor sit amet, consectetur "
- "adipiscing elit posuere.";
- /// @brief a class dedicated to Hooks testing in DHCPv4 server
- ///
- /// This class has a number of static members, because each non-static
- /// method has implicit 'this' parameter, so it does not match callout
- /// signature and couldn't be registered. Furthermore, static methods
- /// can't modify non-static members (for obvious reasons), so many
- /// fields are declared static. It is still better to keep them as
- /// one class rather than unrelated collection of global objects.
- class HooksDhcpv4SrvTest : public Dhcpv4SrvTest {
- public:
- /// @brief creates Dhcpv4Srv and prepares buffers for callouts
- HooksDhcpv4SrvTest() {
- // Allocate new DHCPv6 Server
- srv_ = new NakedDhcpv4Srv(0);
- // clear static buffers
- resetCalloutBuffers();
- }
- /// @brief destructor (deletes Dhcpv4Srv)
- virtual ~HooksDhcpv4SrvTest() {
- HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("buffer4_receive");
- HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("buffer4_send");
- HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("pkt4_receive");
- HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("pkt4_send");
- HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("subnet4_select");
- HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("lease4_renew");
- HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts("lease4_release");
- delete srv_;
- }
- /// @brief creates an option with specified option code
- ///
- /// This method is static, because it is used from callouts
- /// that do not have a pointer to HooksDhcpv4SSrvTest object
- ///
- /// @param option_code code of option to be created
- ///
- /// @return pointer to create option object
- static OptionPtr createOption(uint16_t option_code) {
- char payload[] = {
- 0xa, 0xb, 0xc, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14
- };
- OptionBuffer tmp(payload, payload + sizeof(payload));
- return OptionPtr(new Option(Option::V4, option_code, tmp));
- }
- /// @brief Generates test packet.
- ///
- /// Allocates and generates on-wire buffer that represents test packet, with all
- /// fixed fields set to non-zero values. Content is not always reasonable.
- ///
- /// See generateTestPacket1() function that returns exactly the same packet as
- /// Pkt4 object.
- ///
- /// @return pointer to allocated Pkt4 object
- // Returns a vector containing a DHCPv4 packet header.
- Pkt4Ptr
- generateSimpleDiscover() {
- // That is only part of the header. It contains all "short" fields,
- // larger fields are constructed separately.
- uint8_t hdr[] = {
- 1, 6, 6, 13, // op, htype, hlen, hops,
- 0x12, 0x34, 0x56, 0x78, // transaction-id
- 0, 42, 0x80, 0x00, // 42 secs, BROADCAST flags
- 192, 0, 2, 1, // ciaddr
- 1, 2, 3, 4, // yiaddr
- 192, 0, 2, 255, // siaddr
- 255, 255, 255, 255, // giaddr
- };
- // Initialize the vector with the header fields defined above.
- vector<uint8_t> buf(hdr, hdr + sizeof(hdr));
- // Append the large header fields.
- copy(dummyChaddr, dummyChaddr + Pkt4::MAX_CHADDR_LEN, back_inserter(buf));
- copy(dummySname, dummySname + Pkt4::MAX_SNAME_LEN, back_inserter(buf));
- copy(dummyFile, dummyFile + Pkt4::MAX_FILE_LEN, back_inserter(buf));
- // Should now have all the header, so check. The "static_cast" is used
- // to get round an odd bug whereby the linker appears not to find the
- // definition of DHCPV4_PKT_HDR_LEN if it appears within an EXPECT_EQ().
- EXPECT_EQ(static_cast<size_t>(Pkt4::DHCPV4_PKT_HDR_LEN), buf.size());
- // Add magic cookie
- buf.push_back(0x63);
- buf.push_back(0x82);
- buf.push_back(0x53);
- buf.push_back(0x63);
- // Add message type DISCOVER
- buf.push_back(static_cast<uint8_t>(DHO_DHCP_MESSAGE_TYPE));
- buf.push_back(1); // length (just one byte)
- buf.push_back(static_cast<uint8_t>(DHCPDISCOVER));
- return (Pkt4Ptr(new Pkt4(&buf[0], buf.size())));
- }
- /// Test callback that stores received callout name and pkt4 value
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- buffer4_receive_callout(CalloutHandle& callout_handle) {
- callback_name_ = string("buffer4_receive");
- callout_handle.getArgument("query4", callback_pkt4_);
- callback_argument_names_ = callout_handle.getArgumentNames();
- return (0);
- }
- /// Test callback that changes hwaddr value
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- buffer4_receive_change_hwaddr(CalloutHandle& callout_handle) {
- Pkt4Ptr pkt;
- callout_handle.getArgument("query4", pkt);
- // If there is at least one option with data
- if (pkt->data_.size() >= Pkt4::DHCPV4_PKT_HDR_LEN) {
- // Offset of the first byte of the CHWADDR field. Let's the first
- // byte to some new value that we could later check
- pkt->data_[28] = 0xff;
- }
- // Carry on as usual
- return buffer4_receive_callout(callout_handle);
- }
- /// Test callback that deletes MAC address
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- buffer4_receive_delete_hwaddr(CalloutHandle& callout_handle) {
- Pkt4Ptr pkt;
- callout_handle.getArgument("query4", pkt);
- pkt->data_[2] = 0; // offset 2 is hlen, let's set it to zero
- memset(&pkt->data_[28], 0, Pkt4::MAX_CHADDR_LEN); // Clear CHADDR content
- // carry on as usual
- return buffer4_receive_callout(callout_handle);
- }
- /// Test callback that sets skip flag
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- buffer4_receive_skip(CalloutHandle& callout_handle) {
- callout_handle.setSkip(true);
- // Carry on as usual
- return buffer4_receive_callout(callout_handle);
- }
- /// test callback that stores received callout name and pkt4 value
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- pkt4_receive_callout(CalloutHandle& callout_handle) {
- callback_name_ = string("pkt4_receive");
- callout_handle.getArgument("query4", callback_pkt4_);
- callback_argument_names_ = callout_handle.getArgumentNames();
- return (0);
- }
- /// test callback that changes client-id value
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- pkt4_receive_change_clientid(CalloutHandle& callout_handle) {
- Pkt4Ptr pkt;
- callout_handle.getArgument("query4", pkt);
- // get rid of the old client-id
- pkt->delOption(DHO_DHCP_CLIENT_IDENTIFIER);
- // add a new option
- pkt->addOption(createOption(DHO_DHCP_CLIENT_IDENTIFIER));
- // carry on as usual
- return pkt4_receive_callout(callout_handle);
- }
- /// test callback that deletes client-id
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- pkt4_receive_delete_clientid(CalloutHandle& callout_handle) {
- Pkt4Ptr pkt;
- callout_handle.getArgument("query4", pkt);
- // get rid of the old client-id (and no HWADDR)
- vector<uint8_t> mac;
- pkt->delOption(DHO_DHCP_CLIENT_IDENTIFIER);
- pkt->setHWAddr(1, 0, mac); // HWtype 1, hwardware len = 0
- // carry on as usual
- return pkt4_receive_callout(callout_handle);
- }
- /// test callback that sets skip flag
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- pkt4_receive_skip(CalloutHandle& callout_handle) {
- Pkt4Ptr pkt;
- callout_handle.getArgument("query4", pkt);
- callout_handle.setSkip(true);
- // carry on as usual
- return pkt4_receive_callout(callout_handle);
- }
- /// Test callback that stores received callout name and pkt4 value
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- pkt4_send_callout(CalloutHandle& callout_handle) {
- callback_name_ = string("pkt4_send");
- callout_handle.getArgument("response4", callback_pkt4_);
- callback_argument_names_ = callout_handle.getArgumentNames();
- return (0);
- }
- // Test callback that changes server-id
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- pkt4_send_change_serverid(CalloutHandle& callout_handle) {
- Pkt4Ptr pkt;
- callout_handle.getArgument("response4", pkt);
- // get rid of the old server-id
- pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER);
- // add a new option
- pkt->addOption(createOption(DHO_DHCP_SERVER_IDENTIFIER));
- // carry on as usual
- return pkt4_send_callout(callout_handle);
- }
- /// test callback that deletes server-id
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- pkt4_send_delete_serverid(CalloutHandle& callout_handle) {
- Pkt4Ptr pkt;
- callout_handle.getArgument("response4", pkt);
- // get rid of the old client-id
- pkt->delOption(DHO_DHCP_SERVER_IDENTIFIER);
- // carry on as usual
- return pkt4_send_callout(callout_handle);
- }
- /// Test callback that sets skip flag
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- pkt4_send_skip(CalloutHandle& callout_handle) {
- Pkt4Ptr pkt;
- callout_handle.getArgument("response4", pkt);
- callout_handle.setSkip(true);
- // carry on as usual
- return pkt4_send_callout(callout_handle);
- }
- /// Test callback that stores received callout name and pkt4 value
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- buffer4_send_callout(CalloutHandle& callout_handle) {
- callback_name_ = string("buffer4_send");
- callout_handle.getArgument("response4", callback_pkt4_);
- callback_argument_names_ = callout_handle.getArgumentNames();
- return (0);
- }
- /// Test callback changes the output buffer to a hardcoded value
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- buffer4_send_change_callout(CalloutHandle& callout_handle) {
- Pkt4Ptr pkt;
- callout_handle.getArgument("response4", pkt);
- // modify buffer to set a diffferent payload
- pkt->getBuffer().clear();
- pkt->getBuffer().writeData(dummyFile, sizeof(dummyFile));
- return (0);
- }
- /// Test callback that stores received callout name and pkt4 value
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- skip_callout(CalloutHandle& callout_handle) {
- callout_handle.setSkip(true);
- return (0);
- }
- /// Test callback that stores received callout name and subnet4 values
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- subnet4_select_callout(CalloutHandle& callout_handle) {
- callback_name_ = string("subnet4_select");
- callout_handle.getArgument("query4", callback_pkt4_);
- callout_handle.getArgument("subnet4", callback_subnet4_);
- callout_handle.getArgument("subnet4collection", callback_subnet4collection_);
- callback_argument_names_ = callout_handle.getArgumentNames();
- return (0);
- }
- /// Test callback that picks the other subnet if possible.
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- subnet4_select_different_subnet_callout(CalloutHandle& callout_handle) {
- // Call the basic calllout to record all passed values
- subnet4_select_callout(callout_handle);
- const Subnet4Collection* subnets;
- Subnet4Ptr subnet;
- callout_handle.getArgument("subnet4", subnet);
- callout_handle.getArgument("subnet4collection", subnets);
- // Let's change to a different subnet
- if (subnets->size() > 1) {
- subnet = (*subnets)[1]; // Let's pick the other subnet
- callout_handle.setArgument("subnet4", subnet);
- }
- return (0);
- }
- /// Test callback that stores received callout name passed parameters
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- lease4_release_callout(CalloutHandle& callout_handle) {
- callback_name_ = string("lease4_release");
- callout_handle.getArgument("query4", callback_pkt4_);
- callout_handle.getArgument("lease4", callback_lease4_);
- callback_argument_names_ = callout_handle.getArgumentNames();
- return (0);
- }
- /// Test callback that stores received callout name and subnet4 values
- /// @param callout_handle handle passed by the hooks framework
- /// @return always 0
- static int
- lease4_renew_callout(CalloutHandle& callout_handle) {
- callback_name_ = string("lease4_renew");
- callout_handle.getArgument("subnet4", callback_subnet4_);
- callout_handle.getArgument("lease4", callback_lease4_);
- callout_handle.getArgument("hwaddr", callback_hwaddr_);
- callout_handle.getArgument("clientid", callback_clientid_);
- callback_argument_names_ = callout_handle.getArgumentNames();
- return (0);
- }
- /// resets buffers used to store data received by callouts
- void resetCalloutBuffers() {
- callback_name_ = string("");
- callback_pkt4_.reset();
- callback_lease4_.reset();
- callback_hwaddr_.reset();
- callback_clientid_.reset();
- callback_subnet4_.reset();
- callback_subnet4collection_ = NULL;
- callback_argument_names_.clear();
- }
- /// pointer to Dhcpv4Srv that is used in tests
- NakedDhcpv4Srv* srv_;
- // The following fields are used in testing pkt4_receive_callout
- /// String name of the received callout
- static string callback_name_;
- /// Pkt4 structure returned in the callout
- static Pkt4Ptr callback_pkt4_;
- /// Lease4 structure returned in the callout
- static Lease4Ptr callback_lease4_;
- /// Hardware address returned in the callout
- static HWAddrPtr callback_hwaddr_;
- /// Client-id returned in the callout
- static ClientIdPtr callback_clientid_;
- /// Pointer to a subnet received by callout
- static Subnet4Ptr callback_subnet4_;
- /// A list of all available subnets (received by callout)
- static const Subnet4Collection* callback_subnet4collection_;
- /// A list of all received arguments
- static vector<string> callback_argument_names_;
- };
- // The following fields are used in testing pkt4_receive_callout.
- // See fields description in the class for details
- string HooksDhcpv4SrvTest::callback_name_;
- Pkt4Ptr HooksDhcpv4SrvTest::callback_pkt4_;
- Subnet4Ptr HooksDhcpv4SrvTest::callback_subnet4_;
- HWAddrPtr HooksDhcpv4SrvTest::callback_hwaddr_;
- ClientIdPtr HooksDhcpv4SrvTest::callback_clientid_;
- Lease4Ptr HooksDhcpv4SrvTest::callback_lease4_;
- const Subnet4Collection* HooksDhcpv4SrvTest::callback_subnet4collection_;
- vector<string> HooksDhcpv4SrvTest::callback_argument_names_;
- // Checks if callouts installed on pkt4_receive are indeed called and the
- // all necessary parameters are passed.
- //
- // Note that the test name does not follow test naming convention,
- // but the proper hook name is "buffer4_receive".
- TEST_F(HooksDhcpv4SrvTest, Buffer4ReceiveSimple) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "buffer4_receive", buffer4_receive_callout));
- // Let's create a simple DISCOVER
- Pkt4Ptr dis = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(dis);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered buffer4_receive callback.
- srv_->run();
- // Check that the callback called is indeed the one we installed
- EXPECT_EQ("buffer4_receive", callback_name_);
- // Check that pkt4 argument passing was successful and returned proper value
- EXPECT_TRUE(callback_pkt4_.get() == dis.get());
- // Check that all expected parameters are there
- vector<string> expected_argument_names;
- expected_argument_names.push_back(string("query4"));
- EXPECT_TRUE(expected_argument_names == callback_argument_names_);
- }
- // Checks if callouts installed on buffer4_receive is able to change
- // the values and the parameters are indeed used by the server.
- TEST_F(HooksDhcpv4SrvTest, buffer4RreceiveValueChange) {
- // Install callback that modifies MAC addr of incoming packet
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "buffer4_receive", buffer4_receive_change_hwaddr));
- // Let's create a simple DISCOVER
- Pkt4Ptr discover = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(discover);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive6(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered buffer4_receive callback.
- srv_->run();
- // Check that the server did send a reposonse
- ASSERT_EQ(1, srv_->fake_sent_.size());
- // Make sure that we received a response
- Pkt4Ptr offer = srv_->fake_sent_.front();
- ASSERT_TRUE(offer);
- // Get client-id...
- HWAddrPtr hwaddr = offer->getHWAddr();
- ASSERT_TRUE(hwaddr); // basic sanity check. HWAddr is always present
- // ... and check if it is the modified value
- ASSERT_FALSE(hwaddr->hwaddr_.empty()); // there must be a MAC address
- EXPECT_EQ(0xff, hwaddr->hwaddr_[0]); // check that its first byte was modified
- }
- // Checks if callouts installed on buffer4_receive is able to set skip flag that
- // will cause the server to not parse the packet. Even though the packet is valid,
- // the server should eventually drop it, because there won't be mandatory options
- // (or rather option objects) in it.
- TEST_F(HooksDhcpv4SrvTest, buffer4ReceiveSkip) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "buffer4_receive", buffer4_receive_skip));
- // Let's create a simple DISCOVER
- Pkt4Ptr discover = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(discover);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive6(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered pkt4_receive callback.
- srv_->run();
- // Check that the server dropped the packet and did not produce any response
- ASSERT_EQ(0, srv_->fake_sent_.size());
- }
- // Checks if callouts installed on pkt4_receive are indeed called and the
- // all necessary parameters are passed.
- //
- // Note that the test name does not follow test naming convention,
- // but the proper hook name is "pkt4_receive".
- TEST_F(HooksDhcpv4SrvTest, pkt4ReceiveSimple) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "pkt4_receive", pkt4_receive_callout));
- // Let's create a simple DISCOVER
- Pkt4Ptr sol = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(sol);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered pkt4_receive callback.
- srv_->run();
- // check that the callback called is indeed the one we installed
- EXPECT_EQ("pkt4_receive", callback_name_);
- // check that pkt4 argument passing was successful and returned proper value
- EXPECT_TRUE(callback_pkt4_.get() == sol.get());
- // Check that all expected parameters are there
- vector<string> expected_argument_names;
- expected_argument_names.push_back(string("query4"));
- EXPECT_TRUE(expected_argument_names == callback_argument_names_);
- }
- // Checks if callouts installed on pkt4_received is able to change
- // the values and the parameters are indeed used by the server.
- TEST_F(HooksDhcpv4SrvTest, valueChange_pkt4_receive) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "pkt4_receive", pkt4_receive_change_clientid));
- // Let's create a simple DISCOVER
- Pkt4Ptr sol = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(sol);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered pkt4_receive callback.
- srv_->run();
- // check that the server did send a reposonce
- ASSERT_EQ(1, srv_->fake_sent_.size());
- // Make sure that we received a response
- Pkt4Ptr adv = srv_->fake_sent_.front();
- ASSERT_TRUE(adv);
- // Get client-id...
- OptionPtr clientid = adv->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
- // ... and check if it is the modified value
- OptionPtr expected = createOption(DHO_DHCP_CLIENT_IDENTIFIER);
- EXPECT_TRUE(clientid->equal(expected));
- }
- // Checks if callouts installed on pkt4_received is able to delete
- // existing options and that change impacts server processing (mandatory
- // client-id option is deleted, so the packet is expected to be dropped)
- TEST_F(HooksDhcpv4SrvTest, pkt4ReceiveDeleteClientId) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "pkt4_receive", pkt4_receive_delete_clientid));
- // Let's create a simple DISCOVER
- Pkt4Ptr sol = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(sol);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered pkt4_receive callback.
- srv_->run();
- // Check that the server dropped the packet and did not send a response
- ASSERT_EQ(0, srv_->fake_sent_.size());
- }
- // Checks if callouts installed on pkt4_received is able to set skip flag that
- // will cause the server to not process the packet (drop), even though it is valid.
- TEST_F(HooksDhcpv4SrvTest, pkt4ReceiveSkip) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "pkt4_receive", pkt4_receive_skip));
- // Let's create a simple DISCOVER
- Pkt4Ptr sol = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(sol);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered pkt4_receive callback.
- srv_->run();
- // check that the server dropped the packet and did not produce any response
- ASSERT_EQ(0, srv_->fake_sent_.size());
- }
- // Checks if callouts installed on pkt4_send are indeed called and the
- // all necessary parameters are passed.
- TEST_F(HooksDhcpv4SrvTest, pkt4SendSimple) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "pkt4_send", pkt4_send_callout));
- // Let's create a simple DISCOVER
- Pkt4Ptr sol = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(sol);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered pkt4_receive callback.
- srv_->run();
- // Check that the callback called is indeed the one we installed
- EXPECT_EQ("pkt4_send", callback_name_);
- // Check that there is one packet sent
- ASSERT_EQ(1, srv_->fake_sent_.size());
- Pkt4Ptr adv = srv_->fake_sent_.front();
- // Check that pkt4 argument passing was successful and returned proper value
- EXPECT_TRUE(callback_pkt4_.get() == adv.get());
- // Check that all expected parameters are there
- vector<string> expected_argument_names;
- expected_argument_names.push_back(string("response4"));
- EXPECT_TRUE(expected_argument_names == callback_argument_names_);
- }
- // Checks if callouts installed on pkt4_send is able to change
- // the values and the packet sent contains those changes
- TEST_F(HooksDhcpv4SrvTest, pkt4SendValueChange) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "pkt4_send", pkt4_send_change_serverid));
- // Let's create a simple DISCOVER
- Pkt4Ptr sol = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(sol);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered pkt4_receive callback.
- srv_->run();
- // check that the server did send a reposonce
- ASSERT_EQ(1, srv_->fake_sent_.size());
- // Make sure that we received a response
- Pkt4Ptr adv = srv_->fake_sent_.front();
- ASSERT_TRUE(adv);
- // Get client-id...
- OptionPtr clientid = adv->getOption(DHO_DHCP_SERVER_IDENTIFIER);
- // ... and check if it is the modified value
- OptionPtr expected = createOption(DHO_DHCP_SERVER_IDENTIFIER);
- EXPECT_TRUE(clientid->equal(expected));
- }
- // Checks if callouts installed on pkt4_send is able to delete
- // existing options and that server applies those changes. In particular,
- // we are trying to send a packet without server-id. The packet should
- // be sent
- TEST_F(HooksDhcpv4SrvTest, pkt4SendDeleteServerId) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "pkt4_send", pkt4_send_delete_serverid));
- // Let's create a simple DISCOVER
- Pkt4Ptr sol = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(sol);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered pkt4_receive callback.
- srv_->run();
- // Check that the server indeed sent a malformed ADVERTISE
- ASSERT_EQ(1, srv_->fake_sent_.size());
- // Get that ADVERTISE
- Pkt4Ptr adv = srv_->fake_sent_.front();
- ASSERT_TRUE(adv);
- // Make sure that it does not have server-id
- EXPECT_FALSE(adv->getOption(DHO_DHCP_SERVER_IDENTIFIER));
- }
- // Checks if callouts installed on pkt4_skip is able to set skip flag that
- // will cause the server to not process the packet (drop), even though it is valid.
- TEST_F(HooksDhcpv4SrvTest, skip_pkt4_send) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "pkt4_send", pkt4_send_skip));
- // Let's create a simple REQUEST
- Pkt4Ptr sol = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(sol);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered pkt4_send callback.
- srv_->run();
- // Check that the server sent the message
- ASSERT_EQ(1, srv_->fake_sent_.size());
- // Get the first packet and check that it has zero length (i.e. the server
- // did not do packing on its own)
- Pkt4Ptr sent = srv_->fake_sent_.front();
- EXPECT_EQ(0, sent->getBuffer().getLength());
- }
- // Checks if callouts installed on buffer4_send are indeed called and the
- // all necessary parameters are passed.
- TEST_F(HooksDhcpv4SrvTest, buffer4SendSimple) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "buffer4_send", buffer4_send_callout));
- // Let's create a simple DISCOVER
- Pkt4Ptr discover = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(discover);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered pkt4_receive callback.
- srv_->run();
- // Check that the callback called is indeed the one we installed
- EXPECT_EQ("buffer4_send", callback_name_);
- // Check that there is one packet sent
- ASSERT_EQ(1, srv_->fake_sent_.size());
- Pkt4Ptr adv = srv_->fake_sent_.front();
- // Check that pkt4 argument passing was successful and returned proper value
- EXPECT_TRUE(callback_pkt4_.get() == adv.get());
- // Check that all expected parameters are there
- vector<string> expected_argument_names;
- expected_argument_names.push_back(string("response4"));
- EXPECT_TRUE(expected_argument_names == callback_argument_names_);
- }
- // Checks if callouts installed on buffer4_send are indeed called and that
- // the output buffer can be changed.
- TEST_F(HooksDhcpv4SrvTest, buffer4Send) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "buffer4_send", buffer4_send_change_callout));
- // Let's create a simple DISCOVER
- Pkt4Ptr discover = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(discover);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered pkt4_receive callback.
- srv_->run();
- // Check that there is one packet sent
- ASSERT_EQ(1, srv_->fake_sent_.size());
- Pkt4Ptr adv = srv_->fake_sent_.front();
- // The callout is supposed to fill the output buffer with dummyFile content
- ASSERT_EQ(sizeof(dummyFile), adv->getBuffer().getLength());
- EXPECT_EQ(0, memcmp(adv->getBuffer().getData(), dummyFile, sizeof(dummyFile)));
- }
- // Checks if callouts installed on buffer4_send can set skip flag and that flag
- // causes the packet to not be sent
- TEST_F(HooksDhcpv4SrvTest, buffer4SendSkip) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "buffer4_send", skip_callout));
- // Let's create a simple DISCOVER
- Pkt4Ptr discover = generateSimpleDiscover();
- // Simulate that we have received that traffic
- srv_->fakeReceive(discover);
- // Server will now process to run its normal loop, but instead of calling
- // IfaceMgr::receive4(), it will read all packets from the list set by
- // fakeReceive()
- // In particular, it should call registered pkt4_receive callback.
- srv_->run();
- // Check that there is no packet sent.
- ASSERT_EQ(0, srv_->fake_sent_.size());
- }
- // This test checks if subnet4_select callout is triggered and reports
- // valid parameters
- TEST_F(HooksDhcpv4SrvTest, subnet4SelectSimple) {
- // Install pkt4_receive_callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "subnet4_select", subnet4_select_callout));
- // Configure 2 subnets, both directly reachable over local interface
- // (let's not complicate the matter with relays)
- string config = "{ \"interfaces\": [ \"*\" ],"
- "\"rebind-timer\": 2000, "
- "\"renew-timer\": 1000, "
- "\"subnet4\": [ { "
- " \"pool\": [ \"192.0.2.0/25\" ],"
- " \"subnet\": \"192.0.2.0/24\", "
- " \"interface\": \"" + valid_iface_ + "\" "
- " }, {"
- " \"pool\": [ \"192.0.3.0/25\" ],"
- " \"subnet\": \"192.0.3.0/24\" "
- " } ],"
- "\"valid-lifetime\": 4000 }";
- ElementPtr json = Element::fromJSON(config);
- ConstElementPtr status;
- // Configure the server and make sure the config is accepted
- EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
- ASSERT_TRUE(status);
- comment_ = config::parseAnswer(rcode_, status);
- ASSERT_EQ(0, rcode_);
- // Prepare discover packet. Server should select first subnet for it
- Pkt4Ptr sol = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
- sol->setRemoteAddr(IOAddress("192.0.2.1"));
- sol->setIface(valid_iface_);
- OptionPtr clientid = generateClientId();
- sol->addOption(clientid);
- // Pass it to the server and get an advertise
- Pkt4Ptr adv = srv_->processDiscover(sol);
- // check if we get response at all
- ASSERT_TRUE(adv);
- // Check that the callback called is indeed the one we installed
- EXPECT_EQ("subnet4_select", callback_name_);
- // Check that pkt4 argument passing was successful and returned proper value
- EXPECT_TRUE(callback_pkt4_.get() == sol.get());
- const Subnet4Collection* exp_subnets = CfgMgr::instance().getSubnets4();
- // The server is supposed to pick the first subnet, because of matching
- // interface. Check that the value is reported properly.
- ASSERT_TRUE(callback_subnet4_);
- EXPECT_EQ(exp_subnets->front().get(), callback_subnet4_.get());
- // Server is supposed to report two subnets
- ASSERT_EQ(exp_subnets->size(), callback_subnet4collection_->size());
- // Compare that the available subnets are reported as expected
- EXPECT_TRUE((*exp_subnets)[0].get() == (*callback_subnet4collection_)[0].get());
- EXPECT_TRUE((*exp_subnets)[1].get() == (*callback_subnet4collection_)[1].get());
- }
- // This test checks if callout installed on subnet4_select hook point can pick
- // a different subnet.
- TEST_F(HooksDhcpv4SrvTest, subnet4SelectChange) {
- // Install a callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "subnet4_select", subnet4_select_different_subnet_callout));
- // Configure 2 subnets, both directly reachable over local interface
- // (let's not complicate the matter with relays)
- string config = "{ \"interfaces\": [ \"*\" ],"
- "\"rebind-timer\": 2000, "
- "\"renew-timer\": 1000, "
- "\"subnet4\": [ { "
- " \"pool\": [ \"192.0.2.0/25\" ],"
- " \"subnet\": \"192.0.2.0/24\", "
- " \"interface\": \"" + valid_iface_ + "\" "
- " }, {"
- " \"pool\": [ \"192.0.3.0/25\" ],"
- " \"subnet\": \"192.0.3.0/24\" "
- " } ],"
- "\"valid-lifetime\": 4000 }";
- ElementPtr json = Element::fromJSON(config);
- ConstElementPtr status;
- // Configure the server and make sure the config is accepted
- EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
- ASSERT_TRUE(status);
- comment_ = config::parseAnswer(rcode_, status);
- ASSERT_EQ(0, rcode_);
- // Prepare discover packet. Server should select first subnet for it
- Pkt4Ptr sol = Pkt4Ptr(new Pkt4(DHCPDISCOVER, 1234));
- sol->setRemoteAddr(IOAddress("192.0.2.1"));
- sol->setIface(valid_iface_);
- OptionPtr clientid = generateClientId();
- sol->addOption(clientid);
- // Pass it to the server and get an advertise
- Pkt4Ptr adv = srv_->processDiscover(sol);
- // check if we get response at all
- ASSERT_TRUE(adv);
- // The response should have an address from second pool, so let's check it
- IOAddress addr = adv->getYiaddr();
- EXPECT_NE("0.0.0.0", addr.toText());
- // Get all subnets and use second subnet for verification
- const Subnet4Collection* subnets = CfgMgr::instance().getSubnets4();
- ASSERT_EQ(2, subnets->size());
- // Advertised address must belong to the second pool (in subnet's range,
- // in dynamic pool)
- EXPECT_TRUE((*subnets)[1]->inRange(addr));
- EXPECT_TRUE((*subnets)[1]->inPool(Lease::TYPE_V4, addr));
- }
- // This test verifies that incoming (positive) REQUEST/Renewing can be handled
- // properly and that callout installed on lease4_renew is triggered with
- // expected parameters.
- TEST_F(HooksDhcpv4SrvTest, lease4RenewSimple) {
- const IOAddress addr("192.0.2.106");
- const uint32_t temp_t1 = 50;
- const uint32_t temp_t2 = 75;
- const uint32_t temp_valid = 100;
- const time_t temp_timestamp = time(NULL) - 10;
- // Install a callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "lease4_renew", lease4_renew_callout));
- // Generate client-id also sets client_id_ member
- OptionPtr clientid = generateClientId();
- // Check that the address we are about to use is indeed in pool
- ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
- // let's create a lease and put it in the LeaseMgr
- uint8_t hwaddr2[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
- Lease4Ptr used(new Lease4(IOAddress("192.0.2.106"), hwaddr2, sizeof(hwaddr2),
- &client_id_->getDuid()[0], client_id_->getDuid().size(),
- temp_valid, temp_t1, temp_t2, temp_timestamp,
- subnet_->getID()));
- ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
- // Check that the lease is really in the database
- Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
- ASSERT_TRUE(l);
- // Let's create a RENEW
- Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
- req->setRemoteAddr(IOAddress(addr));
- req->setYiaddr(addr);
- req->setCiaddr(addr); // client's address
- req->addOption(clientid);
- req->addOption(srv_->getServerID());
- // Pass it to the server and hope for a REPLY
- Pkt4Ptr ack = srv_->processRequest(req);
- // Check if we get response at all
- checkResponse(ack, DHCPACK, 1234);
- // Check that the lease is really in the database
- l = checkLease(ack, clientid, req->getHWAddr(), addr);
- ASSERT_TRUE(l);
- // Check that T1, T2, preferred, valid and cltt were really updated
- EXPECT_EQ(l->t1_, subnet_->getT1());
- EXPECT_EQ(l->t2_, subnet_->getT2());
- EXPECT_EQ(l->valid_lft_, subnet_->getValid());
- // Check that the callback called is indeed the one we installed
- EXPECT_EQ("lease4_renew", callback_name_);
- // Check that hwaddr parameter is passed properly
- ASSERT_TRUE(callback_hwaddr_);
- EXPECT_TRUE(*callback_hwaddr_ == *req->getHWAddr());
- // Check that the subnet is passed properly
- ASSERT_TRUE(callback_subnet4_);
- EXPECT_EQ(callback_subnet4_->toText(), subnet_->toText());
- ASSERT_TRUE(callback_clientid_);
- ASSERT_TRUE(client_id_);
- EXPECT_TRUE(*client_id_ == *callback_clientid_);
- // Check if all expected parameters were really received
- vector<string> expected_argument_names;
- expected_argument_names.push_back("subnet4");
- expected_argument_names.push_back("clientid");
- expected_argument_names.push_back("hwaddr");
- expected_argument_names.push_back("lease4");
- sort(callback_argument_names_.begin(), callback_argument_names_.end());
- sort(expected_argument_names.begin(), expected_argument_names.end());
- EXPECT_TRUE(callback_argument_names_ == expected_argument_names);
- EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
- }
- // This test verifies that a callout installed on lease4_renew can trigger
- // the server to not renew a lease.
- TEST_F(HooksDhcpv4SrvTest, lease4RenewSkip) {
- const IOAddress addr("192.0.2.106");
- const uint32_t temp_t1 = 50;
- const uint32_t temp_t2 = 75;
- const uint32_t temp_valid = 100;
- const time_t temp_timestamp = time(NULL) - 10;
- // Install a callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "lease4_renew", skip_callout));
- // Generate client-id also sets client_id_ member
- OptionPtr clientid = generateClientId();
- // Check that the address we are about to use is indeed in pool
- ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
- // let's create a lease and put it in the LeaseMgr
- uint8_t hwaddr2[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
- Lease4Ptr used(new Lease4(IOAddress("192.0.2.106"), hwaddr2, sizeof(hwaddr2),
- &client_id_->getDuid()[0], client_id_->getDuid().size(),
- temp_valid, temp_t1, temp_t2, temp_timestamp,
- subnet_->getID()));
- ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
- // Check that the lease is really in the database
- Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
- ASSERT_TRUE(l);
- // Check that T1, T2, preferred, valid and cltt really set.
- // Constructed lease looks as if it was assigned 10 seconds ago
- // EXPECT_EQ(l->t1_, temp_t1);
- // EXPECT_EQ(l->t2_, temp_t2);
- EXPECT_EQ(l->valid_lft_, temp_valid);
- EXPECT_EQ(l->cltt_, temp_timestamp);
- // Let's create a RENEW
- Pkt4Ptr req = Pkt4Ptr(new Pkt4(DHCPREQUEST, 1234));
- req->setRemoteAddr(IOAddress(addr));
- req->setYiaddr(addr);
- req->setCiaddr(addr); // client's address
- req->addOption(clientid);
- req->addOption(srv_->getServerID());
- // Pass it to the server and hope for a REPLY
- Pkt4Ptr ack = srv_->processRequest(req);
- ASSERT_TRUE(ack);
- // Check that the lease is really in the database
- l = checkLease(ack, clientid, req->getHWAddr(), addr);
- ASSERT_TRUE(l);
- // Check that T1, T2, valid and cltt were NOT updated
- EXPECT_EQ(temp_t1, l->t1_);
- EXPECT_EQ(temp_t2, l->t2_);
- EXPECT_EQ(temp_valid, l->valid_lft_);
- EXPECT_EQ(temp_timestamp, l->cltt_);
- EXPECT_TRUE(LeaseMgrFactory::instance().deleteLease(addr));
- }
- // This test verifies that valid RELEASE triggers lease4_release callouts
- TEST_F(HooksDhcpv4SrvTest, lease4ReleaseSimple) {
- const IOAddress addr("192.0.2.106");
- const uint32_t temp_t1 = 50;
- const uint32_t temp_t2 = 75;
- const uint32_t temp_valid = 100;
- const time_t temp_timestamp = time(NULL) - 10;
- // Install a callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "lease4_release", lease4_release_callout));
- // Generate client-id also duid_
- OptionPtr clientid = generateClientId();
- // Check that the address we are about to use is indeed in pool
- ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
- // Let's create a lease and put it in the LeaseMgr
- uint8_t mac_addr[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
- HWAddrPtr hw(new HWAddr(mac_addr, sizeof(mac_addr), HTYPE_ETHER));
- Lease4Ptr used(new Lease4(addr, mac_addr, sizeof(mac_addr),
- &client_id_->getDuid()[0], client_id_->getDuid().size(),
- temp_valid, temp_t1, temp_t2, temp_timestamp,
- subnet_->getID()));
- ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
- // Check that the lease is really in the database
- Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
- ASSERT_TRUE(l);
- // Let's create a RELEASE
- // Generate client-id also duid_
- Pkt4Ptr rel = Pkt4Ptr(new Pkt4(DHCPRELEASE, 1234));
- rel->setRemoteAddr(addr);
- rel->setCiaddr(addr);
- rel->addOption(clientid);
- rel->addOption(srv_->getServerID());
- rel->setHWAddr(hw);
- // Pass it to the server and hope for a REPLY
- // Note: this is no response to RELEASE in DHCPv4
- EXPECT_NO_THROW(srv_->processRelease(rel));
- // The lease should be gone from LeaseMgr
- l = LeaseMgrFactory::instance().getLease4(addr);
- EXPECT_FALSE(l);
- // Try to get the lease by hardware address
- // @todo: Uncomment this once trac2592 is implemented
- // Lease4Collection leases = LeaseMgrFactory::instance().getLease4(hw->hwaddr_);
- // EXPECT_EQ(leases.size(), 0);
- // Try to get it by hw/subnet_id combination
- l = LeaseMgrFactory::instance().getLease4(hw->hwaddr_, subnet_->getID());
- EXPECT_FALSE(l);
- // Try by client-id
- // @todo: Uncomment this once trac2592 is implemented
- //Lease4Collection leases = LeaseMgrFactory::instance().getLease4(*client_id_);
- //EXPECT_EQ(leases.size(), 0);
- // Try by client-id/subnet-id
- l = LeaseMgrFactory::instance().getLease4(*client_id_, subnet_->getID());
- EXPECT_FALSE(l);
- // Ok, the lease is *really* not there.
- // Check that the callback called is indeed the one we installed
- EXPECT_EQ("lease4_release", callback_name_);
- // Check that pkt4 argument passing was successful and returned proper value
- EXPECT_TRUE(callback_pkt4_.get() == rel.get());
- // Check if all expected parameters were really received
- vector<string> expected_argument_names;
- expected_argument_names.push_back("query4");
- expected_argument_names.push_back("lease4");
- sort(callback_argument_names_.begin(), callback_argument_names_.end());
- sort(expected_argument_names.begin(), expected_argument_names.end());
- EXPECT_TRUE(callback_argument_names_ == expected_argument_names);
- }
- // This test verifies that skip flag returned by a callout installed on the
- // lease4_release hook point will keep the lease
- TEST_F(HooksDhcpv4SrvTest, lease4ReleaseSkip) {
- const IOAddress addr("192.0.2.106");
- const uint32_t temp_t1 = 50;
- const uint32_t temp_t2 = 75;
- const uint32_t temp_valid = 100;
- const time_t temp_timestamp = time(NULL) - 10;
- // Install a callout
- EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
- "lease4_release", skip_callout));
- // Generate client-id also duid_
- OptionPtr clientid = generateClientId();
- // Check that the address we are about to use is indeed in pool
- ASSERT_TRUE(subnet_->inPool(Lease::TYPE_V4, addr));
- // Let's create a lease and put it in the LeaseMgr
- uint8_t mac_addr[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
- HWAddrPtr hw(new HWAddr(mac_addr, sizeof(mac_addr), HTYPE_ETHER));
- Lease4Ptr used(new Lease4(addr, mac_addr, sizeof(mac_addr),
- &client_id_->getDuid()[0], client_id_->getDuid().size(),
- temp_valid, temp_t1, temp_t2, temp_timestamp,
- subnet_->getID()));
- ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
- // Check that the lease is really in the database
- Lease4Ptr l = LeaseMgrFactory::instance().getLease4(addr);
- ASSERT_TRUE(l);
- // Let's create a RELEASE
- // Generate client-id also duid_
- Pkt4Ptr rel = Pkt4Ptr(new Pkt4(DHCPRELEASE, 1234));
- rel->setRemoteAddr(addr);
- rel->setYiaddr(addr);
- rel->addOption(clientid);
- rel->addOption(srv_->getServerID());
- rel->setHWAddr(hw);
- // Pass it to the server and hope for a REPLY
- // Note: this is no response to RELEASE in DHCPv4
- EXPECT_NO_THROW(srv_->processRelease(rel));
- // The lease should be still there
- l = LeaseMgrFactory::instance().getLease4(addr);
- EXPECT_TRUE(l);
- // Try by client-id/subnet-id
- l = LeaseMgrFactory::instance().getLease4(*client_id_, subnet_->getID());
- EXPECT_TRUE(l);
- // Try to get the lease by hardware address
- // @todo: Uncomment this once trac2592 is implemented
- // Lease4Collection leases = LeaseMgrFactory::instance().getLease4(hw->hwaddr_);
- // EXPECT_EQ(leases.size(), 1);
- // Try by client-id
- // @todo: Uncomment this once trac2592 is implemented
- //Lease4Collection leases = LeaseMgrFactory::instance().getLease4(*client_id_);
- //EXPECT_EQ(leases.size(), 1);
- }
- // Checks if server is able to handle a relayed traffic from DOCSIS3.0 modems
- TEST_F(Dhcpv4SrvTest, docsisVendorOptionsParse) {
- // Let's get a traffic capture from DOCSIS3.0 modem
- Pkt4Ptr dis = captureRelayedDiscover();
- ASSERT_NO_THROW(dis->unpack());
- // Check if the packet contain
- OptionPtr opt = dis->getOption(DHO_VIVSO_SUBOPTIONS);
- ASSERT_TRUE(opt);
- boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
- ASSERT_TRUE(vendor);
- // This particular capture that we have included options 1 and 5
- EXPECT_TRUE(vendor->getOption(1));
- EXPECT_TRUE(vendor->getOption(5));
- // It did not include options any other options
- EXPECT_FALSE(vendor->getOption(2));
- EXPECT_FALSE(vendor->getOption(3));
- EXPECT_FALSE(vendor->getOption(17));
- }
- // Checks if server is able to parse incoming docsis option and extract suboption 1 (docsis ORO)
- TEST_F(Dhcpv4SrvTest, docsisVendorORO) {
- // Let's get a traffic capture from DOCSIS3.0 modem
- Pkt4Ptr dis = captureRelayedDiscover();
- EXPECT_NO_THROW(dis->unpack());
- // Check if the packet contains vendor specific information option
- OptionPtr opt = dis->getOption(DHO_VIVSO_SUBOPTIONS);
- ASSERT_TRUE(opt);
- boost::shared_ptr<OptionVendor> vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
- ASSERT_TRUE(vendor);
- opt = vendor->getOption(DOCSIS3_V4_ORO);
- ASSERT_TRUE(opt);
- OptionUint8ArrayPtr oro = boost::dynamic_pointer_cast<OptionUint8Array>(opt);
- EXPECT_TRUE(oro);
- }
- // This test checks if Option Request Option (ORO) in docsis (vendor-id=4491)
- // vendor options is parsed correctly and the requested options are actually assigned.
- TEST_F(Dhcpv4SrvTest, vendorOptionsORO) {
- NakedDhcpv4Srv srv(0);
- ConstElementPtr x;
- string config = "{ \"interfaces\": [ \"all\" ],"
- "\"rebind-timer\": 2000, "
- "\"renew-timer\": 1000, "
- " \"option-data\": [ {"
- " \"name\": \"tftp-servers\","
- " \"space\": \"vendor-4491\","
- " \"code\": 2,"
- " \"data\": \"192.0.2.1, 192.0.2.2\","
- " \"csv-format\": True"
- " }],"
- "\"subnet4\": [ { "
- " \"pool\": [ \"192.0.2.0/25\" ],"
- " \"subnet\": \"192.0.2.0/24\", "
- " \"rebind-timer\": 2000, "
- " \"renew-timer\": 1000, "
- " \"valid-lifetime\": 4000,"
- " \"interface\": \"" + valid_iface_ + "\" "
- " } ],"
- "\"valid-lifetime\": 4000 }";
- ElementPtr json = Element::fromJSON(config);
- EXPECT_NO_THROW(x = configureDhcp4Server(srv, json));
- ASSERT_TRUE(x);
- comment_ = isc::config::parseAnswer(rcode_, x);
- ASSERT_EQ(0, rcode_);
- boost::shared_ptr<Pkt4> dis(new Pkt4(DHCPDISCOVER, 1234));
- // Set the giaddr to non-zero address as if it was relayed.
- dis->setGiaddr(IOAddress("192.0.2.1"));
- OptionPtr clientid = generateClientId();
- dis->addOption(clientid);
- // Pass it to the server and get an advertise
- Pkt4Ptr offer = srv.processDiscover(dis);
- // check if we get response at all
- ASSERT_TRUE(offer);
- // We did not include any vendor opts in DISCOVER, so there should be none
- // in OFFER.
- ASSERT_FALSE(offer->getOption(DHO_VIVSO_SUBOPTIONS));
- // Let's add a vendor-option (vendor-id=4491) with a single sub-option.
- // That suboption has code 1 and is a docsis ORO option.
- boost::shared_ptr<OptionUint8Array> vendor_oro(new OptionUint8Array(Option::V4,
- DOCSIS3_V4_ORO));
- vendor_oro->addValue(DOCSIS3_V4_TFTP_SERVERS); // Request option 33
- OptionPtr vendor(new OptionVendor(Option::V4, 4491));
- vendor->addOption(vendor_oro);
- dis->addOption(vendor);
- // Need to process SOLICIT again after requesting new option.
- offer = srv.processDiscover(dis);
- ASSERT_TRUE(offer);
- // Check if thre is vendor option response
- OptionPtr tmp = offer->getOption(DHO_VIVSO_SUBOPTIONS);
- ASSERT_TRUE(tmp);
- // The response should be OptionVendor object
- boost::shared_ptr<OptionVendor> vendor_resp =
- boost::dynamic_pointer_cast<OptionVendor>(tmp);
- ASSERT_TRUE(vendor_resp);
- OptionPtr docsis2 = vendor_resp->getOption(DOCSIS3_V4_TFTP_SERVERS);
- ASSERT_TRUE(docsis2);
- Option4AddrLstPtr tftp_srvs = boost::dynamic_pointer_cast<Option4AddrLst>(docsis2);
- ASSERT_TRUE(tftp_srvs);
- Option4AddrLst::AddressContainer addrs = tftp_srvs->getAddresses();
- ASSERT_EQ(2, addrs.size());
- EXPECT_EQ("192.0.2.1", addrs[0].toText());
- EXPECT_EQ("192.0.2.2", addrs[1].toText());
- }
- // Test checks whether it is possible to use option definitions defined in
- // src/lib/dhcp/docsis3_option_defs.h.
- TEST_F(Dhcpv4SrvTest, vendorOptionsDocsisDefinitions) {
- ConstElementPtr x;
- string config_prefix = "{ \"interfaces\": [ \"all\" ],"
- "\"rebind-timer\": 2000, "
- "\"renew-timer\": 1000, "
- " \"option-data\": [ {"
- " \"name\": \"tftp-servers\","
- " \"space\": \"vendor-4491\","
- " \"code\": ";
- string config_postfix = ","
- " \"data\": \"192.0.2.1\","
- " \"csv-format\": True"
- " }],"
- "\"subnet4\": [ { "
- " \"pool\": [ \"192.0.2.1 - 192.0.2.50\" ],"
- " \"subnet\": \"192.0.2.0/24\", "
- " \"renew-timer\": 1000, "
- " \"rebind-timer\": 1000, "
- " \"valid-lifetime\": 4000,"
- " \"interface\": \"\""
- " } ],"
- "\"valid-lifetime\": 4000 }";
- // There is docsis3 (vendor-id=4491) vendor option 2, which is a
- // tftp-server. Its format is list of IPv4 addresses.
- string config_valid = config_prefix + "2" + config_postfix;
- // There is no option 99 defined in vendor-id=4491. As there is no
- // definition, the config should fail.
- string config_bogus = config_prefix + "99" + config_postfix;
- ElementPtr json_bogus = Element::fromJSON(config_bogus);
- ElementPtr json_valid = Element::fromJSON(config_valid);
- NakedDhcpv4Srv srv(0);
- // This should fail (missing option definition)
- EXPECT_NO_THROW(x = configureDhcp4Server(srv, json_bogus));
- ASSERT_TRUE(x);
- comment_ = isc::config::parseAnswer(rcode_, x);
- ASSERT_EQ(1, rcode_);
- // This should work (option definition present)
- EXPECT_NO_THROW(x = configureDhcp4Server(srv, json_valid));
- ASSERT_TRUE(x);
- comment_ = isc::config::parseAnswer(rcode_, x);
- ASSERT_EQ(0, rcode_);
- }
- }
- /*I}; // end of isc::dhcp::test namespace
- }; // end of isc::dhcp namespace
- }; // end of isc namespace */
|