config_parser_unittest.cc 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113
  1. // Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <cc/command_interpreter.h>
  16. #include <config/module_spec.h>
  17. #include <dhcp/libdhcp++.h>
  18. #include <dhcp/option6_ia.h>
  19. #include <dhcp/iface_mgr.h>
  20. #include <dhcp/option_custom.h>
  21. #include <dhcp/option_int.h>
  22. #include <dhcp/tests/iface_mgr_test_config.h>
  23. #include <dhcp6/json_config_parser.h>
  24. #include <dhcp6/dhcp6_srv.h>
  25. #include <dhcpsrv/addr_utilities.h>
  26. #include <dhcpsrv/cfgmgr.h>
  27. #include <dhcpsrv/cfg_expiration.h>
  28. #include <dhcpsrv/cfg_hosts.h>
  29. #include <dhcpsrv/subnet.h>
  30. #include <dhcpsrv/subnet_selector.h>
  31. #include <dhcpsrv/testutils/config_result_check.h>
  32. #include <hooks/hooks_manager.h>
  33. #include <defaults.h>
  34. #include "test_data_files_config.h"
  35. #include "test_libraries.h"
  36. #include "marker_file.h"
  37. #include <boost/foreach.hpp>
  38. #include <gtest/gtest.h>
  39. #include <fstream>
  40. #include <iostream>
  41. #include <fstream>
  42. #include <sstream>
  43. #include <string>
  44. #include <vector>
  45. #include <arpa/inet.h>
  46. #include <unistd.h>
  47. using namespace isc;
  48. using namespace isc::asiolink;
  49. using namespace isc::config;
  50. using namespace isc::data;
  51. using namespace isc::dhcp;
  52. using namespace isc::dhcp::test;
  53. using namespace isc::hooks;
  54. using namespace std;
  55. namespace {
  56. std::string specfile(const std::string& name) {
  57. return (std::string(DHCP6_SRC_DIR) + "/" + name);
  58. }
  59. /// @brief Tests that the spec file is valid.
  60. /// Verifies that the DHCP6 configuration specification file is valid.
  61. TEST(Dhcp6SpecTest, basicSpec) {
  62. ASSERT_NO_THROW(isc::config::
  63. moduleSpecFromFile(specfile("dhcp6.spec")));
  64. }
  65. class Dhcp6ParserTest : public ::testing::Test {
  66. protected:
  67. // Check that no hooks libraries are loaded. This is a pre-condition for
  68. // a number of tests, so is checked in one place. As this uses an
  69. // ASSERT call - and it is not clear from the documentation that Gtest
  70. // predicates can be used in a constructor - the check is placed in SetUp.
  71. virtual void SetUp() {
  72. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  73. ASSERT_TRUE(libraries.empty());
  74. }
  75. public:
  76. Dhcp6ParserTest() :rcode_(-1), srv_(0) {
  77. // srv_(0) means to not open any sockets. We don't want to
  78. // deal with sockets here, just check if configuration handling
  79. // is sane.
  80. const IfaceMgr::IfaceCollection& ifaces =
  81. IfaceMgr::instance().getIfaces();
  82. // There must be some interface detected
  83. if (ifaces.empty()) {
  84. // We can't use ASSERT in constructor
  85. ADD_FAILURE() << "No interfaces detected.";
  86. }
  87. valid_iface_ = (*ifaces.begin())->getName();
  88. bogus_iface_ = "nonexisting0";
  89. if (IfaceMgr::instance().getIface(bogus_iface_)) {
  90. ADD_FAILURE() << "The '" << bogus_iface_ << "' exists on this system"
  91. << " while the test assumes that it doesn't, to execute"
  92. << " some negative scenarios. Can't continue this test.";
  93. }
  94. // Reset configuration for each test.
  95. resetConfiguration();
  96. }
  97. ~Dhcp6ParserTest() {
  98. // Reset configuration database after each test.
  99. resetConfiguration();
  100. // ... and delete the hooks library marker files if present
  101. static_cast<void>(remove(LOAD_MARKER_FILE));
  102. static_cast<void>(remove(UNLOAD_MARKER_FILE));
  103. };
  104. // Checks if config_result (result of DHCP server configuration) has
  105. // expected code (0 for success, other for failures).
  106. // Also stores result in rcode_ and comment_.
  107. void checkResult(ConstElementPtr status, int expected_code) {
  108. ASSERT_TRUE(status);
  109. comment_ = parseAnswer(rcode_, status);
  110. EXPECT_EQ(expected_code, rcode_);
  111. }
  112. /// @brief Returns an interface configuration used by the most of the
  113. /// unit tests.
  114. std::string genIfaceConfig() const {
  115. return ("\"interfaces-config\": {"
  116. " \"interfaces\": [ \"*\" ]"
  117. "}");
  118. }
  119. /// @brief Create the simple configuration with single option.
  120. ///
  121. /// This function allows to set one of the parameters that configure
  122. /// option value. These parameters are: "name", "code", "data" and
  123. /// "csv-format".
  124. ///
  125. /// @param param_value string holding option parameter value to be
  126. /// injected into the configuration string.
  127. /// @param parameter name of the parameter to be configured with
  128. /// param value.
  129. std::string createConfigWithOption(const std::string& param_value,
  130. const std::string& parameter) {
  131. std::map<std::string, std::string> params;
  132. if (parameter == "name") {
  133. params["name"] = param_value;
  134. params["space"] = "dhcp6";
  135. params["code"] = "38";
  136. params["data"] = "ABCDEF0105";
  137. params["csv-format"] = "False";
  138. } else if (parameter == "space") {
  139. params["name"] = "subscriber-id";
  140. params["space"] = param_value;
  141. params["code"] = "38";
  142. params["data"] = "ABCDEF0105";
  143. params["csv-format"] = "False";
  144. } else if (parameter == "code") {
  145. params["name"] = "subscriber-id";
  146. params["space"] = "dhcp6";
  147. params["code"] = param_value;
  148. params["data"] = "ABCDEF0105";
  149. params["csv-format"] = "False";
  150. } else if (parameter == "data") {
  151. params["name"] = "subscriber-id";
  152. params["space"] = "dhcp6";
  153. params["code"] = "38";
  154. params["data"] = param_value;
  155. params["csv-format"] = "False";
  156. } else if (parameter == "csv-format") {
  157. params["name"] = "subscriber-id";
  158. params["space"] = "dhcp6";
  159. params["code"] = "38";
  160. params["data"] = "ABCDEF0105";
  161. params["csv-format"] = param_value;
  162. }
  163. return (createConfigWithOption(params));
  164. }
  165. /// @brief Create simple configuration with single option.
  166. ///
  167. /// This function creates a configuration for a single option with
  168. /// custom values for all parameters that describe the option.
  169. ///
  170. /// @params params map holding parameters and their values.
  171. /// @return configuration string containing custom values of parameters
  172. /// describing an option.
  173. std::string createConfigWithOption(const std::map<std::string,
  174. std::string>& params)
  175. {
  176. std::ostringstream stream;
  177. stream << "{ " << genIfaceConfig() << ","
  178. "\"preferred-lifetime\": 3000,"
  179. "\"rebind-timer\": 2000, "
  180. "\"renew-timer\": 1000, "
  181. "\"option-def\": [ {"
  182. " \"name\": \"bool-option\","
  183. " \"code\": 1000,"
  184. " \"type\": \"boolean\","
  185. " \"space\": \"dhcp6\""
  186. "} ],"
  187. "\"subnet6\": [ { "
  188. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  189. " \"subnet\": \"2001:db8:1::/64\", "
  190. " \"option-data\": [ {";
  191. bool first = true;
  192. typedef std::pair<std::string, std::string> ParamPair;
  193. BOOST_FOREACH(ParamPair param, params) {
  194. if (!first) {
  195. stream << ", ";
  196. } else {
  197. // cppcheck-suppress unreadVariable
  198. first = false;
  199. }
  200. if (param.first == "name") {
  201. stream << "\"name\": \"" << param.second << "\"";
  202. } else if (param.first == "space") {
  203. stream << "\"space\": \"" << param.second << "\"";
  204. } else if (param.first == "code") {
  205. stream << "\"code\": " << param.second;;
  206. } else if (param.first == "data") {
  207. stream << "\"data\": \"" << param.second << "\"";
  208. } else if (param.first == "csv-format") {
  209. stream << "\"csv-format\": " << param.second;
  210. }
  211. }
  212. stream <<
  213. " } ]"
  214. " } ],"
  215. "\"valid-lifetime\": 4000 }";
  216. return (stream.str());
  217. }
  218. /// @brief Returns an option from the subnet.
  219. ///
  220. /// This function returns an option from a subnet to which the
  221. /// specified subnet address belongs. The option is identified
  222. /// by its code.
  223. ///
  224. /// @param subnet_address Address which belongs to the subnet from
  225. /// which the option is to be returned.
  226. /// @param option_code Code of the option to be returned.
  227. /// @param expected_options_count Expected number of options in
  228. /// the particular subnet.
  229. ///
  230. /// @return Descriptor of the option. If the descriptor holds a
  231. /// NULL option pointer, it means that there was no such option
  232. /// in the subnet.
  233. OptionDescriptor
  234. getOptionFromSubnet(const IOAddress& subnet_address,
  235. const uint16_t option_code,
  236. const uint16_t expected_options_count = 1) {
  237. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  238. selectSubnet(subnet_address, classify_);
  239. if (!subnet) {
  240. /// @todo replace toText() with the use of operator <<.
  241. ADD_FAILURE() << "A subnet for the specified address "
  242. << subnet_address.toText()
  243. << " does not exist in Config Manager";
  244. }
  245. OptionContainerPtr options =
  246. subnet->getCfgOption()->getAll("dhcp6");
  247. if (expected_options_count != options->size()) {
  248. ADD_FAILURE() << "The number of options in the subnet '"
  249. << subnet_address.toText() << "' is different "
  250. " than expected number of options '"
  251. << expected_options_count << "'";
  252. }
  253. // Get the search index. Index #1 is to search using option code.
  254. const OptionContainerTypeIndex& idx = options->get<1>();
  255. // Get the options for specified index. Expecting one option to be
  256. // returned but in theory we may have multiple options with the same
  257. // code so we get the range.
  258. std::pair<OptionContainerTypeIndex::const_iterator,
  259. OptionContainerTypeIndex::const_iterator> range =
  260. idx.equal_range(option_code);
  261. if (std::distance(range.first, range.second) > 1) {
  262. ADD_FAILURE() << "There is more than one option having the"
  263. " option code '" << option_code << "' in a subnet '"
  264. << subnet_address.toText() << "'. Expected "
  265. " at most one option";
  266. } else if (std::distance(range.first, range.second) == 0) {
  267. return (OptionDescriptor(OptionPtr(), false));
  268. }
  269. return (*range.first);
  270. }
  271. /// @brief Parse and Execute configuration
  272. ///
  273. /// Parses a configuration and executes a configuration of the server.
  274. /// If the operation fails, the current test will register a failure.
  275. ///
  276. /// @param config Configuration to parse
  277. /// @param operation Operation being performed. In the case of an error,
  278. /// the error text will include the string "unable to <operation>.".
  279. ///
  280. /// @return true if the configuration succeeded, false if not. In the
  281. /// latter case, a failure will have been added to the current test.
  282. bool
  283. executeConfiguration(const std::string& config, const char* operation) {
  284. ConstElementPtr status;
  285. try {
  286. ElementPtr json = Element::fromJSON(config);
  287. status = configureDhcp6Server(srv_, json);
  288. } catch (const std::exception& ex) {
  289. ADD_FAILURE() << "Unable to " << operation << ". "
  290. << "The following configuration was used: " << std::endl
  291. << config << std::endl
  292. << " and the following error message was returned:"
  293. << ex.what() << std::endl;
  294. return (false);
  295. }
  296. // The status object must not be NULL
  297. if (!status) {
  298. ADD_FAILURE() << "Unable to " << operation << ". "
  299. << "The configuration function returned a null pointer.";
  300. return (false);
  301. }
  302. // Store the answer if we need it.
  303. // Returned value should be 0 (configuration success)
  304. comment_ = parseAnswer(rcode_, status);
  305. if (rcode_ != 0) {
  306. string reason = "";
  307. if (comment_) {
  308. reason = string(" (") + comment_->stringValue() + string(")");
  309. }
  310. ADD_FAILURE() << "Unable to " << operation << ". "
  311. << "The configuration function returned error code "
  312. << rcode_ << reason;
  313. return (false);
  314. }
  315. return (true);
  316. }
  317. /// @brief Reset configuration database.
  318. ///
  319. /// This function resets configuration data base by removing all subnets
  320. /// option-data, and hooks libraries. The reset must be performed after each
  321. /// test to make sure that contents of the database do not affect the
  322. /// results of subsequent tests.
  323. void resetConfiguration() {
  324. string config = "{ \"interfaces-config\": {"
  325. " \"interfaces\": [ ]"
  326. "},"
  327. "\"hooks-libraries\": [ ],"
  328. "\"preferred-lifetime\": 3000,"
  329. "\"rebind-timer\": 2000, "
  330. "\"renew-timer\": 1000, "
  331. "\"valid-lifetime\": 4000, "
  332. "\"subnet6\": [ ], "
  333. "\"dhcp-ddns\": { \"enable-updates\" : false }, "
  334. "\"option-def\": [ ], "
  335. "\"option-data\": [ ] }";
  336. static_cast<void>(executeConfiguration(config,
  337. "reset configuration database"));
  338. // The default setting is to listen on all interfaces. In order to
  339. // properly test interface configuration we disable listening on
  340. // all interfaces before each test and later check that this setting
  341. // has been overridden by the configuration used in the test.
  342. CfgMgr::instance().clear();
  343. // Create fresh context.
  344. globalContext()->copyContext(ParserContext(Option::V6));
  345. }
  346. /// @brief Test invalid option parameter value.
  347. ///
  348. /// This test function constructs the simple configuration
  349. /// string and injects invalid option configuration into it.
  350. /// It expects that parser will fail with provided option code.
  351. ///
  352. /// @param param_value string holding invalid option parameter value
  353. /// to be injected into configuration string.
  354. /// @param parameter name of the parameter to be configured with
  355. /// param_value (can be any of "name", "code", "data")
  356. void testInvalidOptionParam(const std::string& param_value,
  357. const std::string& parameter) {
  358. ConstElementPtr x;
  359. std::string config = createConfigWithOption(param_value, parameter);
  360. ElementPtr json = Element::fromJSON(config);
  361. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  362. checkResult(x, 1);
  363. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  364. CfgMgr::instance().clear();
  365. }
  366. /// @brief Test invalid option paramater value.
  367. ///
  368. /// This test function constructs the simple configuration
  369. /// string and injects invalid option configuration into it.
  370. /// It expects that parser will fail with provided option code.
  371. ///
  372. /// @param params Map of parameters defining an option.
  373. void
  374. testInvalidOptionParam(const std::map<std::string, std::string>& params) {
  375. ConstElementPtr x;
  376. std::string config = createConfigWithOption(params);
  377. ElementPtr json = Element::fromJSON(config);
  378. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  379. checkResult(x, 1);
  380. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  381. CfgMgr::instance().clear();
  382. }
  383. /// @brief Test option against given code and data.
  384. ///
  385. /// @param option_desc option descriptor that carries the option to
  386. /// be tested.
  387. /// @param expected_code expected code of the option.
  388. /// @param expected_data expected data in the option.
  389. /// @param expected_data_len length of the reference data.
  390. /// @param extra_data if true extra data is allowed in an option
  391. /// after tested data.
  392. void testOption(const OptionDescriptor& option_desc,
  393. uint16_t expected_code, const uint8_t* expected_data,
  394. size_t expected_data_len,
  395. bool extra_data = false) {
  396. // Check if option descriptor contains valid option pointer.
  397. ASSERT_TRUE(option_desc.option_);
  398. // Verify option type.
  399. EXPECT_EQ(expected_code, option_desc.option_->getType());
  400. // We may have many different option types being created. Some of them
  401. // have dedicated classes derived from Option class. In such case if
  402. // we want to verify the option contents against expected_data we have
  403. // to prepare raw buffer with the contents of the option. The easiest
  404. // way is to call pack() which will prepare on-wire data.
  405. util::OutputBuffer buf(option_desc.option_->getData().size());
  406. option_desc.option_->pack(buf);
  407. if (extra_data) {
  408. // The length of the buffer must be at least equal to size of the
  409. // reference data but it can sometimes be greater than that. This is
  410. // because some options carry suboptions that increase the overall
  411. // length.
  412. ASSERT_GE(buf.getLength() - option_desc.option_->getHeaderLen(),
  413. expected_data_len);
  414. } else {
  415. ASSERT_EQ(buf.getLength() - option_desc.option_->getHeaderLen(),
  416. expected_data_len);
  417. }
  418. // Verify that the data is correct. Do not verify suboptions and a header.
  419. const uint8_t* data = static_cast<const uint8_t*>(buf.getData());
  420. EXPECT_EQ(0, memcmp(expected_data, data + option_desc.option_->getHeaderLen(),
  421. expected_data_len));
  422. }
  423. /// @brief Test option configuration.
  424. ///
  425. /// This function creates a configuration for a specified option using
  426. /// a map of parameters specified as the argument. The map holds
  427. /// name/value pairs which identifies option's configuration parameters:
  428. /// - name
  429. /// - space
  430. /// - code
  431. /// - data
  432. /// - csv-format.
  433. /// This function applies a new server configuration and checks that the
  434. /// option being configured is inserted into CfgMgr. The raw contents of
  435. /// this option are compared with the binary data specified as expected
  436. /// data passed to this function.
  437. ///
  438. /// @param params Map of parameters defining an option.
  439. /// @param option_code Option code.
  440. /// @param expected_data Array containing binary data expected to be stored
  441. /// in the configured option.
  442. /// @param expected_data_len Length of the array holding reference data.
  443. void testConfiguration(const std::map<std::string, std::string>& params,
  444. const uint16_t option_code,
  445. const uint8_t* expected_data,
  446. const size_t expected_data_len) {
  447. CfgMgr::instance().clear();
  448. std::string config = createConfigWithOption(params);
  449. ASSERT_TRUE(executeConfiguration(config, "parse option configuration"));
  450. // The subnet should now hold one option with the specified code.
  451. OptionDescriptor desc =
  452. getOptionFromSubnet(IOAddress("2001:db8:1::5"), option_code);
  453. ASSERT_TRUE(desc.option_);
  454. testOption(desc, option_code, expected_data, expected_data_len);
  455. CfgMgr::instance().clear();
  456. }
  457. /// @brief Tests the Rapid Commit configuration for a subnet.
  458. ///
  459. /// This test configures the server with a given configuration and
  460. /// verifies if the Rapid Commit has been configured successfully
  461. /// for a subnet.
  462. ///
  463. /// @param config Server configuration, possibly including the
  464. /// 'rapid-commit' parameter.
  465. /// @param exp_rapid_commit Expected value of the Rapid Commit flag
  466. /// within a subnet.
  467. void testRapidCommit(const std::string& config,
  468. const bool exp_rapid_commit) {
  469. // Clear any existing configuration.
  470. CfgMgr::instance().clear();
  471. // Configure the server.
  472. ElementPtr json = Element::fromJSON(config);
  473. // Make sure that the configuration was successful.
  474. ConstElementPtr status;
  475. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  476. checkResult(status, 0);
  477. // Get the subnet.
  478. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  479. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  480. ASSERT_TRUE(subnet);
  481. // Check the Rapid Commit flag for the subnet.
  482. EXPECT_EQ(exp_rapid_commit, subnet->getRapidCommit());
  483. // Clear any existing configuration.
  484. CfgMgr::instance().clear();
  485. }
  486. int rcode_; ///< Return code (see @ref isc::config::parseAnswer)
  487. Dhcpv6Srv srv_; ///< Instance of the Dhcp6Srv used during tests
  488. ConstElementPtr comment_; ///< Comment (see @ref isc::config::parseAnswer)
  489. string valid_iface_; ///< Valid network interface name (present in system)
  490. string bogus_iface_; ///< invalid network interface name (not in system)
  491. isc::dhcp::ClientClasses classify_; ///< used in client classification
  492. };
  493. // Goal of this test is a verification if a very simple config update
  494. // with just a bumped version number. That's the simplest possible
  495. // config update.
  496. TEST_F(Dhcp6ParserTest, version) {
  497. ConstElementPtr x;
  498. EXPECT_NO_THROW(x = configureDhcp6Server(srv_,
  499. Element::fromJSON("{\"version\": 0}")));
  500. // returned value must be 0 (configuration accepted)
  501. checkResult(x, 0);
  502. }
  503. /// The goal of this test is to verify that the code accepts only
  504. /// valid commands and malformed or unsupported parameters are rejected.
  505. TEST_F(Dhcp6ParserTest, bogusCommand) {
  506. ConstElementPtr x;
  507. EXPECT_NO_THROW(x = configureDhcp6Server(srv_,
  508. Element::fromJSON("{\"bogus\": 5}")));
  509. // returned value must be 1 (configuration parse error)
  510. checkResult(x, 1);
  511. }
  512. /// The goal of this test is to verify if configuration without any
  513. /// subnets defined can be accepted.
  514. TEST_F(Dhcp6ParserTest, emptySubnet) {
  515. ConstElementPtr status;
  516. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  517. Element::fromJSON("{ " + genIfaceConfig() + ","
  518. "\"preferred-lifetime\": 3000,"
  519. "\"rebind-timer\": 2000, "
  520. "\"renew-timer\": 1000, "
  521. "\"subnet6\": [ ], "
  522. "\"valid-lifetime\": 4000 }")));
  523. // returned value should be 0 (success)
  524. checkResult(status, 0);
  525. }
  526. /// The goal of this test is to verify if defined subnet uses global
  527. /// parameter timer definitions.
  528. TEST_F(Dhcp6ParserTest, subnetGlobalDefaults) {
  529. ConstElementPtr status;
  530. string config = "{ " + genIfaceConfig() + ","
  531. "\"preferred-lifetime\": 3000,"
  532. "\"rebind-timer\": 2000, "
  533. "\"renew-timer\": 1000, "
  534. "\"subnet6\": [ { "
  535. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  536. " \"subnet\": \"2001:db8:1::/64\" } ],"
  537. "\"valid-lifetime\": 4000 }";
  538. ElementPtr json = Element::fromJSON(config);
  539. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  540. // check if returned status is OK
  541. checkResult(status, 0);
  542. // Now check if the configuration was indeed handled and we have
  543. // expected pool configured.
  544. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  545. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  546. ASSERT_TRUE(subnet);
  547. EXPECT_EQ(1000, subnet->getT1());
  548. EXPECT_EQ(2000, subnet->getT2());
  549. EXPECT_EQ(3000, subnet->getPreferred());
  550. EXPECT_EQ(4000, subnet->getValid());
  551. // Check that subnet-id is 1
  552. EXPECT_EQ(1, subnet->getID());
  553. }
  554. // This test checks that multiple subnets can be defined and handled properly.
  555. TEST_F(Dhcp6ParserTest, multipleSubnets) {
  556. ConstElementPtr x;
  557. // Collection of four subnets for which ids should be autogenerated
  558. // - ids are unspecified or set to 0.
  559. string config = "{ " + genIfaceConfig() + ","
  560. "\"preferred-lifetime\": 3000,"
  561. "\"rebind-timer\": 2000, "
  562. "\"renew-timer\": 1000, "
  563. "\"subnet6\": [ { "
  564. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  565. " \"subnet\": \"2001:db8:1::/64\" "
  566. " },"
  567. " {"
  568. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  569. " \"subnet\": \"2001:db8:2::/64\", "
  570. " \"id\": 0"
  571. " },"
  572. " {"
  573. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  574. " \"subnet\": \"2001:db8:3::/64\" "
  575. " },"
  576. " {"
  577. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  578. " \"subnet\": \"2001:db8:4::/64\" "
  579. " } ],"
  580. "\"valid-lifetime\": 4000 }";
  581. int cnt = 0; // Number of reconfigurations
  582. ElementPtr json = Element::fromJSON(config);
  583. do {
  584. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  585. checkResult(x, 0);
  586. CfgMgr::instance().commit();
  587. const Subnet6Collection* subnets =
  588. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  589. ASSERT_TRUE(subnets);
  590. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  591. // Check subnet-ids of each subnet (it should be monotonously increasing)
  592. EXPECT_EQ(1, subnets->at(0)->getID());
  593. EXPECT_EQ(2, subnets->at(1)->getID());
  594. EXPECT_EQ(3, subnets->at(2)->getID());
  595. EXPECT_EQ(4, subnets->at(3)->getID());
  596. // Repeat reconfiguration process 10 times and check that the subnet-id
  597. // is set to the same value. Technically, just two iterations would be
  598. // sufficient, but it's nice to have a test that exercises reconfiguration
  599. // a bit.
  600. } while (++cnt < 10);
  601. }
  602. // This checks that it is possible to assign arbitrary ids for subnets.
  603. TEST_F(Dhcp6ParserTest, multipleSubnetsExplicitIDs) {
  604. ConstElementPtr x;
  605. // Four subnets with arbitrary subnet ids.
  606. string config = "{ " + genIfaceConfig() + ","
  607. "\"preferred-lifetime\": 3000,"
  608. "\"rebind-timer\": 2000, "
  609. "\"renew-timer\": 1000, "
  610. "\"subnet6\": [ { "
  611. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  612. " \"subnet\": \"2001:db8:1::/64\", "
  613. " \"id\": 1024"
  614. " },"
  615. " {"
  616. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  617. " \"subnet\": \"2001:db8:2::/64\", "
  618. " \"id\": 100"
  619. " },"
  620. " {"
  621. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  622. " \"subnet\": \"2001:db8:3::/64\", "
  623. " \"id\": 1"
  624. " },"
  625. " {"
  626. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  627. " \"subnet\": \"2001:db8:4::/64\", "
  628. " \"id\": 34"
  629. " } ],"
  630. "\"valid-lifetime\": 4000 }";
  631. int cnt = 0; // Number of reconfigurations
  632. ElementPtr json = Element::fromJSON(config);
  633. do {
  634. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  635. checkResult(x, 0);
  636. CfgMgr::instance().commit();
  637. const Subnet6Collection* subnets =
  638. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  639. ASSERT_TRUE(subnets);
  640. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  641. // Check that subnet ids are as expected.
  642. EXPECT_EQ(1024, subnets->at(0)->getID());
  643. EXPECT_EQ(100, subnets->at(1)->getID());
  644. EXPECT_EQ(1, subnets->at(2)->getID());
  645. EXPECT_EQ(34, subnets->at(3)->getID());
  646. // Repeat reconfiguration process 10 times and check that the subnet-id
  647. // is set to the same value.
  648. } while (++cnt < 3);
  649. }
  650. // CHeck that the configuration with two subnets having the same id is rejected.
  651. TEST_F(Dhcp6ParserTest, multipleSubnetsOverlapingIDs) {
  652. ConstElementPtr x;
  653. // Four subnets, two of them have the same id.
  654. string config = "{ " + genIfaceConfig() + ","
  655. "\"preferred-lifetime\": 3000,"
  656. "\"rebind-timer\": 2000, "
  657. "\"renew-timer\": 1000, "
  658. "\"subnet6\": [ { "
  659. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  660. " \"subnet\": \"2001:db8:1::/64\", "
  661. " \"id\": 1024"
  662. " },"
  663. " {"
  664. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  665. " \"subnet\": \"2001:db8:2::/64\", "
  666. " \"id\": 100"
  667. " },"
  668. " {"
  669. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  670. " \"subnet\": \"2001:db8:3::/64\", "
  671. " \"id\": 1024"
  672. " },"
  673. " {"
  674. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  675. " \"subnet\": \"2001:db8:4::/64\", "
  676. " \"id\": 34"
  677. " } ],"
  678. "\"valid-lifetime\": 4000 }";
  679. ElementPtr json = Element::fromJSON(config);
  680. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  681. checkResult(x, 1);
  682. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  683. }
  684. // Goal of this test is to verify that a previously configured subnet can be
  685. // deleted in subsequent reconfiguration.
  686. TEST_F(Dhcp6ParserTest, reconfigureRemoveSubnet) {
  687. ConstElementPtr x;
  688. // All four subnets
  689. string config4 = "{ " + genIfaceConfig() + ","
  690. "\"preferred-lifetime\": 3000,"
  691. "\"rebind-timer\": 2000, "
  692. "\"renew-timer\": 1000, "
  693. "\"subnet6\": [ { "
  694. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  695. " \"subnet\": \"2001:db8:1::/64\", "
  696. " \"id\": 1"
  697. " },"
  698. " {"
  699. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  700. " \"subnet\": \"2001:db8:2::/64\", "
  701. " \"id\": 2"
  702. " },"
  703. " {"
  704. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  705. " \"subnet\": \"2001:db8:3::/64\", "
  706. " \"id\": 3"
  707. " },"
  708. " {"
  709. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  710. " \"subnet\": \"2001:db8:4::/64\", "
  711. " \"id\": 4"
  712. " } ],"
  713. "\"valid-lifetime\": 4000 }";
  714. // Three subnets (the last one removed)
  715. string config_first3 = "{ " + genIfaceConfig() + ","
  716. "\"preferred-lifetime\": 3000,"
  717. "\"rebind-timer\": 2000, "
  718. "\"renew-timer\": 1000, "
  719. "\"subnet6\": [ { "
  720. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  721. " \"subnet\": \"2001:db8:1::/64\", "
  722. " \"id\": 1"
  723. " },"
  724. " {"
  725. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  726. " \"subnet\": \"2001:db8:2::/64\", "
  727. " \"id\": 2"
  728. " },"
  729. " {"
  730. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  731. " \"subnet\": \"2001:db8:3::/64\", "
  732. " \"id\": 3"
  733. " } ],"
  734. "\"valid-lifetime\": 4000 }";
  735. // Second subnet removed
  736. string config_second_removed = "{ " + genIfaceConfig() + ","
  737. "\"preferred-lifetime\": 3000,"
  738. "\"rebind-timer\": 2000, "
  739. "\"renew-timer\": 1000, "
  740. "\"subnet6\": [ { "
  741. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  742. " \"subnet\": \"2001:db8:1::/64\", "
  743. " \"id\": 1"
  744. " },"
  745. " {"
  746. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  747. " \"subnet\": \"2001:db8:3::/64\", "
  748. " \"id\": 3"
  749. " },"
  750. " {"
  751. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  752. " \"subnet\": \"2001:db8:4::/64\", "
  753. " \"id\": 4"
  754. " } ],"
  755. "\"valid-lifetime\": 4000 }";
  756. // CASE 1: Configure 4 subnets, then reconfigure and remove the
  757. // last one.
  758. ElementPtr json = Element::fromJSON(config4);
  759. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  760. checkResult(x, 0);
  761. CfgMgr::instance().commit();
  762. const Subnet6Collection* subnets =
  763. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  764. ASSERT_TRUE(subnets);
  765. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  766. // Do the reconfiguration (the last subnet is removed)
  767. json = Element::fromJSON(config_first3);
  768. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  769. checkResult(x, 0);
  770. CfgMgr::instance().commit();
  771. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  772. ASSERT_TRUE(subnets);
  773. ASSERT_EQ(3, subnets->size()); // We expect 3 subnets now (4th is removed)
  774. EXPECT_EQ(1, subnets->at(0)->getID());
  775. EXPECT_EQ(2, subnets->at(1)->getID());
  776. EXPECT_EQ(3, subnets->at(2)->getID());
  777. /// CASE 2: Configure 4 subnets, then reconfigure and remove one
  778. /// from in between (not first, not last)
  779. json = Element::fromJSON(config4);
  780. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  781. checkResult(x, 0);
  782. CfgMgr::instance().commit();
  783. // Do reconfiguration
  784. json = Element::fromJSON(config_second_removed);
  785. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  786. checkResult(x, 0);
  787. CfgMgr::instance().commit();
  788. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  789. ASSERT_TRUE(subnets);
  790. ASSERT_EQ(3, subnets->size()); // We expect 4 subnets
  791. EXPECT_EQ(1, subnets->at(0)->getID());
  792. // The second subnet (with subnet-id = 2) is no longer there
  793. EXPECT_EQ(3, subnets->at(1)->getID());
  794. EXPECT_EQ(4, subnets->at(2)->getID());
  795. }
  796. // This test checks if it is possible to override global values
  797. // on a per subnet basis.
  798. TEST_F(Dhcp6ParserTest, subnetLocal) {
  799. ConstElementPtr status;
  800. string config = "{ " + genIfaceConfig() + ","
  801. "\"preferred-lifetime\": 3000,"
  802. "\"rebind-timer\": 2000, "
  803. "\"renew-timer\": 1000, "
  804. "\"subnet6\": [ { "
  805. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  806. " \"renew-timer\": 1, "
  807. " \"rebind-timer\": 2, "
  808. " \"preferred-lifetime\": 3,"
  809. " \"valid-lifetime\": 4,"
  810. " \"subnet\": \"2001:db8:1::/64\" } ],"
  811. "\"valid-lifetime\": 4000 }";
  812. ElementPtr json = Element::fromJSON(config);
  813. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  814. // returned value should be 0 (configuration success)
  815. checkResult(status, 0);
  816. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  817. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  818. ASSERT_TRUE(subnet);
  819. EXPECT_EQ(1, subnet->getT1());
  820. EXPECT_EQ(2, subnet->getT2());
  821. EXPECT_EQ(3, subnet->getPreferred());
  822. EXPECT_EQ(4, subnet->getValid());
  823. }
  824. // This test checks if it is possible to define a subnet with an
  825. // interface defined.
  826. TEST_F(Dhcp6ParserTest, subnetInterface) {
  827. ConstElementPtr status;
  828. // There should be at least one interface
  829. string config = "{ " + genIfaceConfig() + ","
  830. "\"preferred-lifetime\": 3000,"
  831. "\"rebind-timer\": 2000, "
  832. "\"renew-timer\": 1000, "
  833. "\"subnet6\": [ { "
  834. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  835. " \"interface\": \"" + valid_iface_ + "\","
  836. " \"subnet\": \"2001:db8:1::/64\" } ],"
  837. "\"valid-lifetime\": 4000 }";
  838. cout << config << endl;
  839. ElementPtr json = Element::fromJSON(config);
  840. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  841. // returned value should be 0 (configuration success)
  842. checkResult(status, 0);
  843. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  844. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  845. ASSERT_TRUE(subnet);
  846. EXPECT_EQ(valid_iface_, subnet->getIface());
  847. }
  848. // This test checks if invalid interface name will be rejected in
  849. // Subnet6 definition.
  850. TEST_F(Dhcp6ParserTest, subnetInterfaceBogus) {
  851. ConstElementPtr status;
  852. // There should be at least one interface
  853. string config = "{ " + genIfaceConfig() + ","
  854. "\"preferred-lifetime\": 3000,"
  855. "\"rebind-timer\": 2000, "
  856. "\"renew-timer\": 1000, "
  857. "\"subnet6\": [ { "
  858. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  859. " \"interface\": \"" + bogus_iface_ + "\","
  860. " \"subnet\": \"2001:db8:1::/64\" } ],"
  861. "\"valid-lifetime\": 4000 }";
  862. cout << config << endl;
  863. ElementPtr json = Element::fromJSON(config);
  864. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  865. // returned value should be 1 (configuration error)
  866. checkResult(status, 1);
  867. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  868. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  869. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  870. EXPECT_FALSE(subnet);
  871. }
  872. // This test checks if it is not allowed to define global interface
  873. // parameter.
  874. TEST_F(Dhcp6ParserTest, interfaceGlobal) {
  875. ConstElementPtr status;
  876. string config = "{ " + genIfaceConfig() + ","
  877. "\"preferred-lifetime\": 3000,"
  878. "\"rebind-timer\": 2000, "
  879. "\"renew-timer\": 1000, "
  880. "\"interface\": \"" + valid_iface_ + "\"," // Not valid. Can be defined in subnet only
  881. "\"subnet6\": [ { "
  882. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  883. " \"subnet\": \"2001:db8:1::/64\" } ],"
  884. "\"valid-lifetime\": 4000 }";
  885. cout << config << endl;
  886. ElementPtr json = Element::fromJSON(config);
  887. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  888. // returned value should be 1 (parse error)
  889. checkResult(status, 1);
  890. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  891. }
  892. // This test checks if it is possible to define a subnet with an
  893. // interface-id option defined.
  894. TEST_F(Dhcp6ParserTest, subnetInterfaceId) {
  895. const string valid_interface_id = "foobar";
  896. const string bogus_interface_id = "blah";
  897. // There should be at least one interface
  898. const string config = "{ "
  899. "\"preferred-lifetime\": 3000,"
  900. "\"rebind-timer\": 2000, "
  901. "\"renew-timer\": 1000, "
  902. "\"subnet6\": [ { "
  903. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  904. " \"interface-id\": \"" + valid_interface_id + "\","
  905. " \"subnet\": \"2001:db8:1::/64\" } ],"
  906. "\"valid-lifetime\": 4000 }";
  907. ElementPtr json = Element::fromJSON(config);
  908. ConstElementPtr status;
  909. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  910. // Returned value should be 0 (configuration success)
  911. checkResult(status, 0);
  912. // Try to get a subnet based on bogus interface-id option
  913. OptionBuffer tmp(bogus_interface_id.begin(), bogus_interface_id.end());
  914. SubnetSelector selector;
  915. selector.first_relay_linkaddr_ = IOAddress("5000::1");
  916. selector.interface_id_.reset(new Option(Option::V6, D6O_INTERFACE_ID, tmp));
  917. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  918. selectSubnet(selector);
  919. EXPECT_FALSE(subnet);
  920. // Now try to get subnet for valid interface-id value
  921. tmp = OptionBuffer(valid_interface_id.begin(), valid_interface_id.end());
  922. selector.interface_id_.reset(new Option(Option::V6, D6O_INTERFACE_ID, tmp));
  923. subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  924. selectSubnet(selector);
  925. ASSERT_TRUE(subnet);
  926. EXPECT_TRUE(selector.interface_id_->equals(subnet->getInterfaceId()));
  927. }
  928. // This test checks if it is not allowed to define global interface
  929. // parameter.
  930. TEST_F(Dhcp6ParserTest, interfaceIdGlobal) {
  931. const string config = "{ " + genIfaceConfig() + ","
  932. "\"preferred-lifetime\": 3000,"
  933. "\"rebind-timer\": 2000, "
  934. "\"renew-timer\": 1000, "
  935. "\"interface-id\": \"foobar\"," // Not valid. Can be defined in subnet only
  936. "\"subnet6\": [ { "
  937. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  938. " \"subnet\": \"2001:db8:1::/64\" } ],"
  939. "\"valid-lifetime\": 4000 }";
  940. ElementPtr json = Element::fromJSON(config);
  941. ConstElementPtr status;
  942. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  943. // Returned value should be 1 (parse error)
  944. checkResult(status, 1);
  945. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  946. }
  947. // This test checks if it is not possible to define a subnet with an
  948. // interface (i.e. local subnet) and interface-id (remote subnet) defined.
  949. TEST_F(Dhcp6ParserTest, subnetInterfaceAndInterfaceId) {
  950. const string config = "{ \"preferred-lifetime\": 3000,"
  951. "\"rebind-timer\": 2000, "
  952. "\"renew-timer\": 1000, "
  953. "\"subnet6\": [ { "
  954. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  955. " \"interface\": \"" + valid_iface_ + "\","
  956. " \"interface-id\": \"foobar\","
  957. " \"subnet\": \"2001:db8:1::/64\" } ],"
  958. "\"valid-lifetime\": 4000 }";
  959. ElementPtr json = Element::fromJSON(config);
  960. ConstElementPtr status;
  961. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  962. // Returned value should be 1 (configuration error)
  963. checkResult(status, 1);
  964. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  965. }
  966. // This test checks the configuration of the Rapid Commit option
  967. // support for the subnet.
  968. TEST_F(Dhcp6ParserTest, subnetRapidCommit) {
  969. {
  970. // rapid-commit implicitly set to false.
  971. SCOPED_TRACE("Default Rapid Commit setting");
  972. testRapidCommit("{ \"preferred-lifetime\": 3000,"
  973. "\"rebind-timer\": 2000, "
  974. "\"renew-timer\": 1000, "
  975. "\"subnet6\": [ { "
  976. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - "
  977. "2001:db8:1::ffff\" } ],"
  978. " \"subnet\": \"2001:db8:1::/64\" } ],"
  979. "\"valid-lifetime\": 4000 }",
  980. false);
  981. }
  982. {
  983. // rapid-commit explicitly set to true.
  984. SCOPED_TRACE("Enable Rapid Commit");
  985. testRapidCommit("{ \"preferred-lifetime\": 3000,"
  986. "\"rebind-timer\": 2000, "
  987. "\"renew-timer\": 1000, "
  988. "\"subnet6\": [ { "
  989. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - "
  990. "2001:db8:1::ffff\" } ],"
  991. " \"rapid-commit\": True,"
  992. " \"subnet\": \"2001:db8:1::/64\" } ],"
  993. "\"valid-lifetime\": 4000 }",
  994. true);
  995. }
  996. {
  997. // rapid-commit explicitly set to false.
  998. SCOPED_TRACE("Disable Rapid Commit");
  999. testRapidCommit("{ \"preferred-lifetime\": 3000,"
  1000. "\"rebind-timer\": 2000, "
  1001. "\"renew-timer\": 1000, "
  1002. "\"subnet6\": [ { "
  1003. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - "
  1004. "2001:db8:1::ffff\" } ],"
  1005. " \"rapid-commit\": False,"
  1006. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1007. "\"valid-lifetime\": 4000 }",
  1008. false);
  1009. }
  1010. }
  1011. // This test checks that multiple pools can be defined and handled properly.
  1012. // The test defines 2 subnets, each with 2 pools.
  1013. TEST_F(Dhcp6ParserTest, multiplePools) {
  1014. // Collection with two subnets, each with 2 pools.
  1015. string config = "{ " + genIfaceConfig() + ","
  1016. "\"preferred-lifetime\": 3000,"
  1017. "\"rebind-timer\": 2000, "
  1018. "\"renew-timer\": 1000, "
  1019. "\"subnet6\": [ { "
  1020. " \"pools\": [ "
  1021. " { \"pool\": \"2001:db8:1::/96\" },"
  1022. " { \"pool\": \"2001:db8:1:0:abcd::/112\" }"
  1023. " ],"
  1024. " \"subnet\": \"2001:db8:1::/64\" "
  1025. " },"
  1026. " {"
  1027. " \"pools\": [ "
  1028. " { \"pool\": \"2001:db8:2::1 - 2001:db8:2::ff\" },"
  1029. " { \"pool\": \"2001:db8:2::300 - 2001:db8:2::3ff\" }"
  1030. " ],"
  1031. " \"subnet\": \"2001:db8:2::/64\""
  1032. " } ],"
  1033. "\"valid-lifetime\": 4000 }";
  1034. ElementPtr json;
  1035. ASSERT_NO_THROW(json = Element::fromJSON(config));
  1036. ConstElementPtr status;
  1037. ASSERT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1038. checkResult(status, 0);
  1039. const Subnet6Collection* subnets =
  1040. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  1041. ASSERT_TRUE(subnets);
  1042. ASSERT_EQ(2, subnets->size()); // We expect 2 subnets
  1043. // Check the first subnet
  1044. const PoolCollection& pools1 = subnets->at(0)->getPools(Lease::TYPE_NA);
  1045. ASSERT_EQ(2, pools1.size());
  1046. EXPECT_EQ("type=IA_NA, 2001:db8:1::-2001:db8:1::ffff:ffff, delegated_len=128",
  1047. pools1[0]->toText());
  1048. EXPECT_EQ("type=IA_NA, 2001:db8:1:0:abcd::-2001:db8:1:0:abcd::ffff, delegated_len=128",
  1049. pools1[1]->toText());
  1050. // There shouldn't be any TA or PD pools
  1051. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_TA).empty());
  1052. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_PD).empty());
  1053. // Check the second subnet
  1054. const PoolCollection& pools2 = subnets->at(1)->getPools(Lease::TYPE_NA);
  1055. ASSERT_EQ(2, pools2.size());
  1056. EXPECT_EQ("type=IA_NA, 2001:db8:2::1-2001:db8:2::ff, delegated_len=128",
  1057. pools2[0]->toText());
  1058. EXPECT_EQ("type=IA_NA, 2001:db8:2::300-2001:db8:2::3ff, delegated_len=128",
  1059. pools2[1]->toText());
  1060. // There shouldn't be any TA or PD pools
  1061. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_TA).empty());
  1062. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_PD).empty());
  1063. }
  1064. // Test verifies that a subnet with pool values that do not belong to that
  1065. // pool are rejected.
  1066. TEST_F(Dhcp6ParserTest, poolOutOfSubnet) {
  1067. ConstElementPtr status;
  1068. string config = "{ " + genIfaceConfig() + ","
  1069. "\"preferred-lifetime\": 3000,"
  1070. "\"rebind-timer\": 2000, "
  1071. "\"renew-timer\": 1000, "
  1072. "\"subnet6\": [ { "
  1073. " \"pools\": [ { \"pool\": \"4001:db8:1::/80\" } ],"
  1074. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1075. "\"valid-lifetime\": 4000 }";
  1076. ElementPtr json = Element::fromJSON(config);
  1077. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1078. // returned value must be 1 (values error)
  1079. // as the pool does not belong to that subnet
  1080. checkResult(status, 1);
  1081. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1082. }
  1083. // Goal of this test is to verify if pools can be defined
  1084. // using prefix/length notation. There is no separate test for min-max
  1085. // notation as it was tested in several previous tests.
  1086. // Note this test also verifies that subnets can be configured without
  1087. // prefix delegation pools.
  1088. TEST_F(Dhcp6ParserTest, poolPrefixLen) {
  1089. ConstElementPtr x;
  1090. string config = "{ " + genIfaceConfig() + ","
  1091. "\"preferred-lifetime\": 3000,"
  1092. "\"rebind-timer\": 2000, "
  1093. "\"renew-timer\": 1000, "
  1094. "\"subnet6\": [ { "
  1095. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  1096. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1097. "\"valid-lifetime\": 4000 }";
  1098. ElementPtr json = Element::fromJSON(config);
  1099. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1100. // returned value must be 1 (configuration parse error)
  1101. checkResult(x, 0);
  1102. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1103. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1104. ASSERT_TRUE(subnet);
  1105. EXPECT_EQ(1000, subnet->getT1());
  1106. EXPECT_EQ(2000, subnet->getT2());
  1107. EXPECT_EQ(3000, subnet->getPreferred());
  1108. EXPECT_EQ(4000, subnet->getValid());
  1109. }
  1110. // Goal of this test is to verify the basic parsing of a prefix delegation
  1111. // pool. It uses a single, valid pd pool.
  1112. TEST_F(Dhcp6ParserTest, pdPoolBasics) {
  1113. ConstElementPtr x;
  1114. // Define a single valid pd pool.
  1115. string config =
  1116. "{ " + genIfaceConfig() + ","
  1117. "\"preferred-lifetime\": 3000,"
  1118. "\"rebind-timer\": 2000, "
  1119. "\"renew-timer\": 1000, "
  1120. "\"subnet6\": [ { "
  1121. " \"subnet\": \"2001:db8:1::/64\","
  1122. " \"pd-pools\": ["
  1123. " { \"prefix\": \"2001:db8:1::\", "
  1124. " \"prefix-len\": 64, "
  1125. " \"delegated-len\": 128"
  1126. " } ],"
  1127. "\"valid-lifetime\": 4000 }"
  1128. "] }";
  1129. // Convert the JSON string into Elements.
  1130. ElementPtr json;
  1131. ASSERT_NO_THROW(json = Element::fromJSON(config));
  1132. // Verify that DHCP6 configuration processing succeeds.
  1133. // Returned value must be non-empty ConstElementPtr to config result.
  1134. // rcode should be 0 which indicates successful configuration processing.
  1135. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1136. checkResult(x, 0);
  1137. // Test that we can retrieve the subnet.
  1138. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1139. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1140. ASSERT_TRUE(subnet);
  1141. // Fetch the collection of PD pools. It should have 1 entry.
  1142. PoolCollection pc;
  1143. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_PD));
  1144. EXPECT_EQ(1, pc.size());
  1145. // Get a pointer to the pd pool instance, and verify its contents.
  1146. Pool6Ptr p6;
  1147. ASSERT_NO_THROW(p6 = boost::dynamic_pointer_cast<Pool6>(pc[0]));
  1148. ASSERT_TRUE(p6);
  1149. EXPECT_EQ("2001:db8:1::", p6->getFirstAddress().toText());
  1150. EXPECT_EQ(128, p6->getLength());
  1151. // prefix-len is not directly accessible after pool construction, so
  1152. // verify that it was interpreted correctly by checking the last address
  1153. // value.
  1154. isc::asiolink::IOAddress prefixAddress("2001:db8:1::");
  1155. EXPECT_EQ(lastAddrInPrefix(prefixAddress, 64), p6->getLastAddress());
  1156. }
  1157. // Goal of this test is verify that a list of PD pools can be configured.
  1158. // It also verifies that a subnet may be configured with both regular pools
  1159. // and pd pools.
  1160. TEST_F(Dhcp6ParserTest, pdPoolList) {
  1161. ConstElementPtr x;
  1162. // We will configure three pools of prefixes for the subnet. Note that
  1163. // the 3rd prefix is out of the subnet prefix (the prefix doesn't match
  1164. // the subnet prefix).
  1165. const char* prefixes[] = {
  1166. "2001:db8:1:1::",
  1167. "2001:db8:1:2::",
  1168. "3000:1:3::"
  1169. };
  1170. string config =
  1171. "{ " + genIfaceConfig() + ","
  1172. "\"preferred-lifetime\": 3000,"
  1173. "\"rebind-timer\": 2000, "
  1174. "\"renew-timer\": 1000, "
  1175. "\"subnet6\": [ { "
  1176. " \"pools\": [ { \"pool\": \"2001:db8:1:04::/80\" } ],"
  1177. " \"subnet\": \"2001:db8:1::/40\","
  1178. " \"pd-pools\": ["
  1179. " { \"prefix\": \"2001:db8:1:01::\", "
  1180. " \"prefix-len\": 72, "
  1181. " \"delegated-len\": 80"
  1182. " },"
  1183. " { \"prefix\": \"2001:db8:1:02::\", "
  1184. " \"prefix-len\": 72, "
  1185. " \"delegated-len\": 88"
  1186. " },"
  1187. " { \"prefix\": \"3000:1:03::\", "
  1188. " \"prefix-len\": 72, "
  1189. " \"delegated-len\": 96"
  1190. " }"
  1191. "],"
  1192. "\"valid-lifetime\": 4000 }"
  1193. "] }";
  1194. // Convert the JSON string into Elements.
  1195. ElementPtr json = Element::fromJSON(config);
  1196. ASSERT_NO_THROW(json = Element::fromJSON(config));
  1197. // Verify that DHCP6 configuration processing succeeds.
  1198. // Returned value must be non-empty ConstElementPtr to config result.
  1199. // rcode should be 0 which indicates successful configuration processing.
  1200. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1201. checkResult(x, 0);
  1202. // Test that we can retrieve the subnet.
  1203. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1204. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1205. ASSERT_TRUE(subnet);
  1206. // Fetch the collection of NA pools. It should have 1 entry.
  1207. PoolCollection pc;
  1208. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_NA));
  1209. EXPECT_EQ(1, pc.size());
  1210. // Fetch the collection of PD pools. It should have 3 entries.
  1211. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_PD));
  1212. EXPECT_EQ(3, pc.size());
  1213. // Loop through the pools and verify their contents.
  1214. for (int i = 0; i < 3; i++) {
  1215. Pool6Ptr p6;
  1216. ASSERT_NO_THROW(p6 = boost::dynamic_pointer_cast<Pool6>(pc[i]));
  1217. ASSERT_TRUE(p6);
  1218. EXPECT_EQ(prefixes[i], p6->getFirstAddress().toText());
  1219. EXPECT_EQ((80 + (i * 8)), p6->getLength());
  1220. }
  1221. }
  1222. // Goal of this test is to verify the a whole prefix can be delegated and that
  1223. // a whole subnet can be delegated.
  1224. TEST_F(Dhcp6ParserTest, subnetAndPrefixDelegated) {
  1225. ConstElementPtr x;
  1226. // Define a single valid pd pool.
  1227. string config =
  1228. "{ " + genIfaceConfig() + ","
  1229. "\"preferred-lifetime\": 3000,"
  1230. "\"rebind-timer\": 2000, "
  1231. "\"renew-timer\": 1000, "
  1232. "\"subnet6\": [ { "
  1233. " \"subnet\": \"2001:db8:1::/64\","
  1234. " \"pd-pools\": ["
  1235. " { \"prefix\": \"2001:db8:1::\", "
  1236. " \"prefix-len\": 64, "
  1237. " \"delegated-len\": 64"
  1238. " } ],"
  1239. "\"valid-lifetime\": 4000 }"
  1240. "] }";
  1241. // Convert the JSON string into Elements.
  1242. ElementPtr json;
  1243. ASSERT_NO_THROW(json = Element::fromJSON(config));
  1244. // Verify that DHCP6 configuration processing succeeds.
  1245. // Returned value must be non-empty ConstElementPtr to config result.
  1246. // rcode should be 0 which indicates successful configuration processing.
  1247. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1248. checkResult(x, 0);
  1249. // Test that we can retrieve the subnet.
  1250. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1251. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1252. ASSERT_TRUE(subnet);
  1253. // Fetch the collection of PD pools. It should have 1 entry.
  1254. PoolCollection pc;
  1255. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_PD));
  1256. EXPECT_EQ(1, pc.size());
  1257. // Get a pointer to the pd pool instance, and verify its contents.
  1258. Pool6Ptr p6;
  1259. ASSERT_NO_THROW(p6 = boost::dynamic_pointer_cast<Pool6>(pc[0]));
  1260. ASSERT_TRUE(p6);
  1261. EXPECT_EQ("2001:db8:1::", p6->getFirstAddress().toText());
  1262. EXPECT_EQ(64, p6->getLength());
  1263. // prefix-len is not directly accessible after pool construction, so
  1264. // verify that it was interpreted correctly by checking the last address
  1265. // value.
  1266. isc::asiolink::IOAddress prefixAddress("2001:db8:1::");
  1267. EXPECT_EQ(lastAddrInPrefix(prefixAddress, 64), p6->getLastAddress());
  1268. }
  1269. // Goal of this test is check for proper handling of invalid prefix delegation
  1270. // pool configuration. It uses an array of invalid configurations to check
  1271. // a variety of configuration errors.
  1272. TEST_F(Dhcp6ParserTest, invalidPdPools) {
  1273. ConstElementPtr x;
  1274. const char *config[] = {
  1275. // No prefix.
  1276. "{ \"interfaces-config\": { },"
  1277. "\"preferred-lifetime\": 3000,"
  1278. "\"rebind-timer\": 2000, "
  1279. "\"renew-timer\": 1000, "
  1280. "\"subnet6\": [ { "
  1281. " \"subnet\": \"2001:db8:1::/64\","
  1282. " \"pd-pools\": ["
  1283. " { "
  1284. " \"prefix-len\": 64, "
  1285. " \"delegated-len\": 128"
  1286. " } ],"
  1287. "\"valid-lifetime\": 4000 }"
  1288. "] }",
  1289. // No prefix-len.
  1290. "{ \"interfaces-config\": { },"
  1291. "\"preferred-lifetime\": 3000,"
  1292. "\"rebind-timer\": 2000, "
  1293. "\"renew-timer\": 1000, "
  1294. "\"subnet6\": [ { "
  1295. " \"subnet\": \"2001:db8:1::/64\","
  1296. " \"pd-pools\": ["
  1297. " { \"prefix\": \"2001:db8:1::\", "
  1298. " \"delegated-len\": 128"
  1299. " } ],"
  1300. "\"valid-lifetime\": 4000 }"
  1301. "] }",
  1302. // No delegated-len.
  1303. "{ \"interfaces-config\": { },"
  1304. "\"preferred-lifetime\": 3000,"
  1305. "\"rebind-timer\": 2000, "
  1306. "\"renew-timer\": 1000, "
  1307. "\"subnet6\": [ { "
  1308. " \"subnet\": \"2001:db8:1::/64\","
  1309. " \"pd-pools\": ["
  1310. " { \"prefix\": \"2001:db8:1::\", "
  1311. " \"prefix-len\": 64 "
  1312. " } ],"
  1313. "\"valid-lifetime\": 4000 }"
  1314. "] }",
  1315. // Delegated length is too short.
  1316. "{ \"interfaces-config\": { },"
  1317. "\"preferred-lifetime\": 3000,"
  1318. "\"rebind-timer\": 2000, "
  1319. "\"renew-timer\": 1000, "
  1320. "\"subnet6\": [ { "
  1321. " \"subnet\": \"2001:db8:1::/64\","
  1322. " \"pd-pools\": ["
  1323. " { \"prefix\": \"2001:db8:1::\", "
  1324. " \"prefix-len\": 128, "
  1325. " \"delegated-len\": 64"
  1326. " } ],"
  1327. "\"valid-lifetime\": 4000 }"
  1328. "] }"
  1329. };
  1330. ElementPtr json;
  1331. int num_msgs = sizeof(config)/sizeof(char*);
  1332. for (int i = 0; i < num_msgs; i++) {
  1333. // Convert JSON string to Elements.
  1334. ASSERT_NO_THROW(json = Element::fromJSON(config[i]));
  1335. // Configuration processing should fail without a throw.
  1336. ASSERT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1337. // Returned value must be non-empty ConstElementPtr to config result.
  1338. // rcode should be 1 which indicates configuration error.
  1339. checkResult(x, 1);
  1340. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  1341. }
  1342. }
  1343. // The goal of this test is to check whether an option definition
  1344. // that defines an option carrying an IPv6 address can be created.
  1345. TEST_F(Dhcp6ParserTest, optionDefIpv6Address) {
  1346. // Configuration string.
  1347. std::string config =
  1348. "{ \"option-def\": [ {"
  1349. " \"name\": \"foo\","
  1350. " \"code\": 100,"
  1351. " \"type\": \"ipv6-address\","
  1352. " \"space\": \"isc\""
  1353. " } ]"
  1354. "}";
  1355. ElementPtr json = Element::fromJSON(config);
  1356. // Make sure that the particular option definition does not exist.
  1357. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1358. getCfgOptionDef()->get("isc", 100);
  1359. ASSERT_FALSE(def);
  1360. // Use the configuration string to create new option definition.
  1361. ConstElementPtr status;
  1362. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1363. ASSERT_TRUE(status);
  1364. checkResult(status, 0);
  1365. // We need to commit option definitions because later in this test we
  1366. // will be checking if they get removed when "option-def" parameter
  1367. // is removed from a configuration.
  1368. LibDHCP::commitRuntimeOptionDefs();
  1369. // The option definition should now be available in the CfgMgr.
  1370. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1371. ASSERT_TRUE(def);
  1372. // Verify that the option definition data is valid.
  1373. EXPECT_EQ("foo", def->getName());
  1374. EXPECT_EQ(100, def->getCode());
  1375. EXPECT_FALSE(def->getArrayType());
  1376. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, def->getType());
  1377. // The copy of the option definition should be available in the libdhcp++.
  1378. OptionDefinitionPtr def_libdhcp = LibDHCP::getRuntimeOptionDef("isc", 100);
  1379. ASSERT_TRUE(def_libdhcp);
  1380. // Both definitions should be held in distinct pointers but they should
  1381. // be equal.
  1382. EXPECT_TRUE(def_libdhcp != def);
  1383. EXPECT_TRUE(*def_libdhcp == *def);
  1384. // Let's apply empty configuration. This removes the option definitions
  1385. // configuration and should result in removal of the option 100 from the
  1386. // libdhcp++.
  1387. config = "{ }";
  1388. json = Element::fromJSON(config);
  1389. ASSERT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1390. checkResult(status, 0);
  1391. EXPECT_FALSE(LibDHCP::getRuntimeOptionDef("isc", 100));
  1392. }
  1393. // The goal of this test is to check whether an option definition
  1394. // that defines an option carrying a record of data fields can
  1395. // be created.
  1396. TEST_F(Dhcp6ParserTest, optionDefRecord) {
  1397. // Configuration string.
  1398. std::string config =
  1399. "{ \"option-def\": [ {"
  1400. " \"name\": \"foo\","
  1401. " \"code\": 100,"
  1402. " \"type\": \"record\","
  1403. " \"record-types\": \"uint16, ipv4-address, ipv6-address, string\","
  1404. " \"space\": \"isc\""
  1405. " } ]"
  1406. "}";
  1407. ElementPtr json = Element::fromJSON(config);
  1408. // Make sure that the particular option definition does not exist.
  1409. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1410. getCfgOptionDef()->get("isc", 100);
  1411. ASSERT_FALSE(def);
  1412. // Use the configuration string to create new option definition.
  1413. ConstElementPtr status;
  1414. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1415. ASSERT_TRUE(status);
  1416. checkResult(status, 0);
  1417. // The option definition should now be available in the CfgMgr.
  1418. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1419. ASSERT_TRUE(def);
  1420. // Check the option data.
  1421. EXPECT_EQ("foo", def->getName());
  1422. EXPECT_EQ(100, def->getCode());
  1423. EXPECT_EQ(OPT_RECORD_TYPE, def->getType());
  1424. EXPECT_FALSE(def->getArrayType());
  1425. // The option comprises the record of data fields. Verify that all
  1426. // fields are present and they are of the expected types.
  1427. const OptionDefinition::RecordFieldsCollection& record_fields =
  1428. def->getRecordFields();
  1429. ASSERT_EQ(4, record_fields.size());
  1430. EXPECT_EQ(OPT_UINT16_TYPE, record_fields[0]);
  1431. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, record_fields[1]);
  1432. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, record_fields[2]);
  1433. EXPECT_EQ(OPT_STRING_TYPE, record_fields[3]);
  1434. }
  1435. // The goal of this test is to verify that multiple option definitions
  1436. // can be created.
  1437. TEST_F(Dhcp6ParserTest, optionDefMultiple) {
  1438. // Configuration string.
  1439. std::string config =
  1440. "{ \"option-def\": [ {"
  1441. " \"name\": \"foo\","
  1442. " \"code\": 100,"
  1443. " \"type\": \"uint32\","
  1444. " \"space\": \"isc\""
  1445. " },"
  1446. " {"
  1447. " \"name\": \"foo-2\","
  1448. " \"code\": 101,"
  1449. " \"type\": \"ipv4-address\","
  1450. " \"space\": \"isc\""
  1451. " } ]"
  1452. "}";
  1453. ElementPtr json = Element::fromJSON(config);
  1454. // Make sure that the option definitions do not exist yet.
  1455. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1456. getCfgOptionDef()->get("isc", 100));
  1457. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1458. getCfgOptionDef()->get("isc", 101));
  1459. // Use the configuration string to create new option definitions.
  1460. ConstElementPtr status;
  1461. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1462. ASSERT_TRUE(status);
  1463. checkResult(status, 0);
  1464. // Check the first definition we have created.
  1465. OptionDefinitionPtr def1 = CfgMgr::instance().getStagingCfg()->
  1466. getCfgOptionDef()->get("isc", 100);
  1467. ASSERT_TRUE(def1);
  1468. // Check the option data.
  1469. EXPECT_EQ("foo", def1->getName());
  1470. EXPECT_EQ(100, def1->getCode());
  1471. EXPECT_EQ(OPT_UINT32_TYPE, def1->getType());
  1472. EXPECT_FALSE(def1->getArrayType());
  1473. // Check the second option definition we have created.
  1474. OptionDefinitionPtr def2 = CfgMgr::instance().getStagingCfg()->
  1475. getCfgOptionDef()->get("isc", 101);
  1476. ASSERT_TRUE(def2);
  1477. // Check the option data.
  1478. EXPECT_EQ("foo-2", def2->getName());
  1479. EXPECT_EQ(101, def2->getCode());
  1480. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def2->getType());
  1481. EXPECT_FALSE(def2->getArrayType());
  1482. }
  1483. // The goal of this test is to verify that the duplicated option
  1484. // definition is not accepted.
  1485. TEST_F(Dhcp6ParserTest, optionDefDuplicate) {
  1486. // Preconfigure libdhcp++ with option definitions. The new configuration
  1487. // should override it, but when the new configuration fails, it should
  1488. // revert to this original configuration.
  1489. OptionDefSpaceContainer defs;
  1490. OptionDefinitionPtr def(new OptionDefinition("bar", 233, "string"));
  1491. defs.addItem(def, "isc");
  1492. LibDHCP::setRuntimeOptionDefs(defs);
  1493. LibDHCP::commitRuntimeOptionDefs();
  1494. // Configuration string. Both option definitions have
  1495. // the same code and belong to the same option space.
  1496. // This configuration should not be accepted.
  1497. std::string config =
  1498. "{ \"option-def\": [ {"
  1499. " \"name\": \"foo\","
  1500. " \"code\": 100,"
  1501. " \"type\": \"uint32\","
  1502. " \"space\": \"isc\""
  1503. " },"
  1504. " {"
  1505. " \"name\": \"foo-2\","
  1506. " \"code\": 100,"
  1507. " \"type\": \"ipv4-address\","
  1508. " \"space\": \"isc\""
  1509. " } ]"
  1510. "}";
  1511. ElementPtr json = Element::fromJSON(config);
  1512. // Make sure that the option definition does not exist yet.
  1513. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1514. getCfgOptionDef()->get("isc", 100));
  1515. // Use the configuration string to create new option definitions.
  1516. ConstElementPtr status;
  1517. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1518. ASSERT_TRUE(status);
  1519. checkResult(status, 1);
  1520. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1521. // The new configuration should have inserted option 100, but
  1522. // once configuration failed (on the duplicate option definition)
  1523. // the original configuration in libdhcp++ should be reverted.
  1524. EXPECT_FALSE(LibDHCP::getRuntimeOptionDef("isc", 100));
  1525. def = LibDHCP::getRuntimeOptionDef("isc", 233);
  1526. ASSERT_TRUE(def);
  1527. EXPECT_EQ("bar", def->getName());
  1528. EXPECT_EQ(233, def->getCode());
  1529. }
  1530. // The goal of this test is to verify that the option definition
  1531. // comprising an array of uint32 values can be created.
  1532. TEST_F(Dhcp6ParserTest, optionDefArray) {
  1533. // Configuration string. Created option definition should
  1534. // comprise an array of uint32 values.
  1535. std::string config =
  1536. "{ \"option-def\": [ {"
  1537. " \"name\": \"foo\","
  1538. " \"code\": 100,"
  1539. " \"type\": \"uint32\","
  1540. " \"array\": True,"
  1541. " \"space\": \"isc\""
  1542. " } ]"
  1543. "}";
  1544. ElementPtr json = Element::fromJSON(config);
  1545. // Make sure that the particular option definition does not exist.
  1546. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1547. getCfgOptionDef()->get("isc", 100);
  1548. ASSERT_FALSE(def);
  1549. // Use the configuration string to create new option definition.
  1550. ConstElementPtr status;
  1551. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1552. ASSERT_TRUE(status);
  1553. checkResult(status, 0);
  1554. // The option definition should now be available in the CfgMgr.
  1555. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1556. ASSERT_TRUE(def);
  1557. // Check the option data.
  1558. EXPECT_EQ("foo", def->getName());
  1559. EXPECT_EQ(100, def->getCode());
  1560. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  1561. EXPECT_TRUE(def->getArrayType());
  1562. }
  1563. // The purpose of this test to verify that encapsulated option
  1564. // space name may be specified.
  1565. TEST_F(Dhcp6ParserTest, optionDefEncapsulate) {
  1566. // Configuration string. Included the encapsulated
  1567. // option space name.
  1568. std::string config =
  1569. "{ \"option-def\": [ {"
  1570. " \"name\": \"foo\","
  1571. " \"code\": 100,"
  1572. " \"type\": \"uint32\","
  1573. " \"space\": \"isc\","
  1574. " \"encapsulate\": \"sub-opts-space\""
  1575. " } ]"
  1576. "}";
  1577. ElementPtr json = Element::fromJSON(config);
  1578. // Make sure that the particular option definition does not exist.
  1579. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1580. getCfgOptionDef()->get("isc", 100);
  1581. ASSERT_FALSE(def);
  1582. // Use the configuration string to create new option definition.
  1583. ConstElementPtr status;
  1584. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1585. ASSERT_TRUE(status);
  1586. checkResult(status, 0);
  1587. // The option definition should now be available in the CfgMgr.
  1588. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1589. ASSERT_TRUE(def);
  1590. // Check the option data.
  1591. EXPECT_EQ("foo", def->getName());
  1592. EXPECT_EQ(100, def->getCode());
  1593. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  1594. EXPECT_FALSE(def->getArrayType());
  1595. EXPECT_EQ("sub-opts-space", def->getEncapsulatedSpace());
  1596. }
  1597. /// The purpose of this test is to verify that the option definition
  1598. /// with invalid name is not accepted.
  1599. TEST_F(Dhcp6ParserTest, optionDefInvalidName) {
  1600. // Configuration string. The option name is invalid as it
  1601. // contains the % character.
  1602. std::string config =
  1603. "{ \"option-def\": [ {"
  1604. " \"name\": \"invalid%name\","
  1605. " \"code\": 100,"
  1606. " \"type\": \"string\","
  1607. " \"space\": \"isc\""
  1608. " } ]"
  1609. "}";
  1610. ElementPtr json = Element::fromJSON(config);
  1611. // Use the configuration string to create new option definition.
  1612. ConstElementPtr status;
  1613. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1614. ASSERT_TRUE(status);
  1615. // Expecting parsing error (error code 1).
  1616. checkResult(status, 1);
  1617. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1618. }
  1619. /// The purpose of this test is to verify that the option definition
  1620. /// with invalid type is not accepted.
  1621. TEST_F(Dhcp6ParserTest, optionDefInvalidType) {
  1622. // Configuration string. The option type is invalid. It is
  1623. // "sting" instead of "string".
  1624. std::string config =
  1625. "{ \"option-def\": [ {"
  1626. " \"name\": \"foo\","
  1627. " \"code\": 100,"
  1628. " \"type\": \"sting\","
  1629. " \"space\": \"isc\""
  1630. " } ]"
  1631. "}";
  1632. ElementPtr json = Element::fromJSON(config);
  1633. // Use the configuration string to create new option definition.
  1634. ConstElementPtr status;
  1635. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1636. ASSERT_TRUE(status);
  1637. // Expecting parsing error (error code 1).
  1638. checkResult(status, 1);
  1639. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1640. }
  1641. /// The purpose of this test is to verify that the option definition
  1642. /// with invalid type is not accepted.
  1643. TEST_F(Dhcp6ParserTest, optionDefInvalidRecordType) {
  1644. // Configuration string. The third of the record fields
  1645. // is invalid. It is "sting" instead of "string".
  1646. std::string config =
  1647. "{ \"option-def\": [ {"
  1648. " \"name\": \"foo\","
  1649. " \"code\": 100,"
  1650. " \"type\": \"record\","
  1651. " \"record-types\": \"uint32,uint8,sting\","
  1652. " \"space\": \"isc\""
  1653. " } ]"
  1654. "}";
  1655. ElementPtr json = Element::fromJSON(config);
  1656. // Use the configuration string to create new option definition.
  1657. ConstElementPtr status;
  1658. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1659. ASSERT_TRUE(status);
  1660. // Expecting parsing error (error code 1).
  1661. checkResult(status, 1);
  1662. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1663. }
  1664. /// The goal of this test is to verify that the invalid encapsulated
  1665. /// option space name is not accepted.
  1666. TEST_F(Dhcp6ParserTest, optionDefInvalidEncapsulatedSpace) {
  1667. // Configuration string. The encapsulated option space
  1668. // name is invalid (% character is not allowed).
  1669. std::string config =
  1670. "{ \"option-def\": [ {"
  1671. " \"name\": \"foo\","
  1672. " \"code\": 100,"
  1673. " \"type\": \"uint32\","
  1674. " \"space\": \"isc\","
  1675. " \"encapsulate\": \"invalid%space%name\""
  1676. " } ]"
  1677. "}";
  1678. ElementPtr json = Element::fromJSON(config);
  1679. // Use the configuration string to create new option definition.
  1680. ConstElementPtr status;
  1681. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1682. ASSERT_TRUE(status);
  1683. // Expecting parsing error (error code 1).
  1684. checkResult(status, 1);
  1685. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1686. }
  1687. /// The goal of this test is to verify that the encapsulated
  1688. /// option space name can't be specified for the option that
  1689. /// comprises an array of data fields.
  1690. TEST_F(Dhcp6ParserTest, optionDefEncapsulatedSpaceAndArray) {
  1691. // Configuration string. The encapsulated option space
  1692. // name is set to non-empty value and the array flag
  1693. // is set.
  1694. std::string config =
  1695. "{ \"option-def\": [ {"
  1696. " \"name\": \"foo\","
  1697. " \"code\": 100,"
  1698. " \"type\": \"uint32\","
  1699. " \"array\": True,"
  1700. " \"space\": \"isc\","
  1701. " \"encapsulate\": \"valid-space-name\""
  1702. " } ]"
  1703. "}";
  1704. ElementPtr json = Element::fromJSON(config);
  1705. // Use the configuration string to create new option definition.
  1706. ConstElementPtr status;
  1707. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1708. ASSERT_TRUE(status);
  1709. // Expecting parsing error (error code 1).
  1710. checkResult(status, 1);
  1711. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1712. }
  1713. /// The goal of this test is to verify that the option may not
  1714. /// encapsulate option space it belongs to.
  1715. TEST_F(Dhcp6ParserTest, optionDefEncapsulateOwnSpace) {
  1716. // Configuration string. Option is set to encapsulate
  1717. // option space it belongs to.
  1718. std::string config =
  1719. "{ \"option-def\": [ {"
  1720. " \"name\": \"foo\","
  1721. " \"code\": 100,"
  1722. " \"type\": \"uint32\","
  1723. " \"space\": \"isc\","
  1724. " \"encapsulate\": \"isc\""
  1725. " } ]"
  1726. "}";
  1727. ElementPtr json = Element::fromJSON(config);
  1728. // Use the configuration string to create new option definition.
  1729. ConstElementPtr status;
  1730. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1731. ASSERT_TRUE(status);
  1732. // Expecting parsing error (error code 1).
  1733. checkResult(status, 1);
  1734. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1735. }
  1736. /// The purpose of this test is to verify that it is not allowed
  1737. /// to override the standard option (that belongs to dhcp6 option
  1738. /// space and has its definition) and that it is allowed to define
  1739. /// option in the dhcp6 option space that has a code which is not
  1740. /// used by any of the standard options.
  1741. TEST_F(Dhcp6ParserTest, optionStandardDefOverride) {
  1742. // Configuration string. The option code 100 is unassigned
  1743. // so it can be used for a custom option definition in
  1744. // dhcp6 option space.
  1745. std::string config =
  1746. "{ \"option-def\": [ {"
  1747. " \"name\": \"foo\","
  1748. " \"code\": 100,"
  1749. " \"type\": \"string\","
  1750. " \"space\": \"dhcp6\""
  1751. " } ]"
  1752. "}";
  1753. ElementPtr json = Element::fromJSON(config);
  1754. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1755. getCfgOptionDef()->get("dhcp6", 100);
  1756. ASSERT_FALSE(def);
  1757. // Use the configuration string to create new option definition.
  1758. ConstElementPtr status;
  1759. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1760. ASSERT_TRUE(status);
  1761. checkResult(status, 0);
  1762. // The option definition should now be available in the CfgMgr.
  1763. def = CfgMgr::instance().getStagingCfg()->
  1764. getCfgOptionDef()->get("dhcp6", 100);
  1765. ASSERT_TRUE(def);
  1766. // Check the option data.
  1767. EXPECT_EQ("foo", def->getName());
  1768. EXPECT_EQ(100, def->getCode());
  1769. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  1770. EXPECT_FALSE(def->getArrayType());
  1771. // The combination of option space and code is invalid. The 'dhcp6'
  1772. // option space groups standard options and the code 3 is reserved
  1773. // for one of them.
  1774. config =
  1775. "{ \"option-def\": [ {"
  1776. " \"name\": \"foo\","
  1777. " \"code\": 3,"
  1778. " \"type\": \"string\","
  1779. " \"space\": \"dhcp6\""
  1780. " } ]"
  1781. "}";
  1782. json = Element::fromJSON(config);
  1783. // Use the configuration string to create new option definition.
  1784. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1785. ASSERT_TRUE(status);
  1786. // Expecting parsing error (error code 1).
  1787. checkResult(status, 1);
  1788. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1789. /// @todo The option 63 is a standard DHCPv6 option. However, at this point
  1790. /// there is no definition for this option in libdhcp++, so it should be
  1791. /// allowed to define it from the configuration interface. This test will
  1792. /// have to be removed once definitions for remaining standard options are
  1793. /// created.
  1794. config =
  1795. "{ \"option-def\": [ {"
  1796. " \"name\": \"geolocation\","
  1797. " \"code\": 63,"
  1798. " \"type\": \"string\","
  1799. " \"space\": \"dhcp6\""
  1800. " } ]"
  1801. "}";
  1802. json = Element::fromJSON(config);
  1803. // Use the configuration string to create new option definition.
  1804. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1805. ASSERT_TRUE(status);
  1806. // Expecting success.
  1807. checkResult(status, 0);
  1808. def = CfgMgr::instance().getStagingCfg()->
  1809. getCfgOptionDef()->get("dhcp6", 63);
  1810. ASSERT_TRUE(def);
  1811. // Check the option data.
  1812. EXPECT_EQ("geolocation", def->getName());
  1813. EXPECT_EQ(63, def->getCode());
  1814. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  1815. EXPECT_FALSE(def->getArrayType());
  1816. }
  1817. // Goal of this test is to verify that global option
  1818. // data is configured for the subnet if the subnet
  1819. // configuration does not include options configuration.
  1820. TEST_F(Dhcp6ParserTest, optionDataDefaults) {
  1821. ConstElementPtr x;
  1822. string config = "{ " + genIfaceConfig() + ","
  1823. "\"preferred-lifetime\": 3000,"
  1824. "\"rebind-timer\": 2000,"
  1825. "\"renew-timer\": 1000,"
  1826. "\"option-data\": [ {"
  1827. " \"name\": \"subscriber-id\","
  1828. " \"data\": \"ABCDEF0105\","
  1829. " \"csv-format\": False"
  1830. " },"
  1831. " {"
  1832. " \"name\": \"preference\","
  1833. " \"data\": \"01\""
  1834. " } ],"
  1835. "\"subnet6\": [ { "
  1836. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  1837. " \"subnet\": \"2001:db8:1::/64\""
  1838. " } ],"
  1839. "\"valid-lifetime\": 4000 }";
  1840. ElementPtr json = Element::fromJSON(config);
  1841. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1842. checkResult(x, 0);
  1843. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1844. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1845. ASSERT_TRUE(subnet);
  1846. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  1847. ASSERT_EQ(2, options->size());
  1848. // Get the search index. Index #1 is to search using option code.
  1849. const OptionContainerTypeIndex& idx = options->get<1>();
  1850. // Get the options for specified index. Expecting one option to be
  1851. // returned but in theory we may have multiple options with the same
  1852. // code so we get the range.
  1853. std::pair<OptionContainerTypeIndex::const_iterator,
  1854. OptionContainerTypeIndex::const_iterator> range =
  1855. idx.equal_range(D6O_SUBSCRIBER_ID);
  1856. // Expect single option with the code equal to 38.
  1857. ASSERT_EQ(1, std::distance(range.first, range.second));
  1858. const uint8_t subid_expected[] = {
  1859. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  1860. };
  1861. // Check if option is valid in terms of code and carried data.
  1862. testOption(*range.first, D6O_SUBSCRIBER_ID, subid_expected,
  1863. sizeof(subid_expected));
  1864. range = idx.equal_range(D6O_PREFERENCE);
  1865. ASSERT_EQ(1, std::distance(range.first, range.second));
  1866. // Do another round of testing with second option.
  1867. const uint8_t pref_expected[] = {
  1868. 0x01
  1869. };
  1870. testOption(*range.first, D6O_PREFERENCE, pref_expected,
  1871. sizeof(pref_expected));
  1872. // Check that options with other option codes are not returned.
  1873. for (uint16_t code = 47; code < 57; ++code) {
  1874. range = idx.equal_range(code);
  1875. EXPECT_EQ(0, std::distance(range.first, range.second));
  1876. }
  1877. }
  1878. /// The goal of this test is to verify that two options having the same
  1879. /// option code can be added to different option spaces.
  1880. TEST_F(Dhcp6ParserTest, optionDataTwoSpaces) {
  1881. // This configuration string is to configure two options
  1882. // sharing the code 56 and having different definitions
  1883. // and belonging to the different option spaces.
  1884. // The option definition must be provided for the
  1885. // option that belongs to the 'isc' option space.
  1886. // The definition is not required for the option that
  1887. // belongs to the 'dhcp6' option space as it is the
  1888. // standard option.
  1889. string config = "{ " + genIfaceConfig() + ","
  1890. "\"preferred-lifetime\": 3000,"
  1891. "\"valid-lifetime\": 4000,"
  1892. "\"rebind-timer\": 2000,"
  1893. "\"renew-timer\": 1000,"
  1894. "\"option-data\": [ {"
  1895. " \"name\": \"subscriber-id\","
  1896. " \"data\": \"ABCDEF0105\","
  1897. " \"csv-format\": False"
  1898. " },"
  1899. " {"
  1900. " \"name\": \"foo\","
  1901. " \"space\": \"isc\","
  1902. " \"data\": \"1234\""
  1903. " } ],"
  1904. "\"option-def\": [ {"
  1905. " \"name\": \"foo\","
  1906. " \"code\": 38,"
  1907. " \"type\": \"uint32\","
  1908. " \"space\": \"isc\""
  1909. " } ],"
  1910. "\"subnet6\": [ { "
  1911. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  1912. " \"subnet\": \"2001:db8:1::/64\""
  1913. " } ]"
  1914. "}";
  1915. ConstElementPtr status;
  1916. ElementPtr json = Element::fromJSON(config);
  1917. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1918. ASSERT_TRUE(status);
  1919. checkResult(status, 0);
  1920. // Options should be now available for the subnet.
  1921. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1922. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1923. ASSERT_TRUE(subnet);
  1924. // Try to get the option from the space dhcp6.
  1925. OptionDescriptor desc1 = subnet->getCfgOption()->get("dhcp6", 38);
  1926. ASSERT_TRUE(desc1.option_);
  1927. EXPECT_EQ(38, desc1.option_->getType());
  1928. // Try to get the option from the space isc.
  1929. OptionDescriptor desc2 = subnet->getCfgOption()->get("isc", 38);
  1930. ASSERT_TRUE(desc2.option_);
  1931. EXPECT_EQ(38, desc1.option_->getType());
  1932. // Try to get the non-existing option from the non-existing
  1933. // option space and expect that option is not returned.
  1934. OptionDescriptor desc3 = subnet->getCfgOption()->get("non-existing", 38);
  1935. ASSERT_FALSE(desc3.option_);
  1936. }
  1937. // The goal of this test is to verify that it is possible to
  1938. // encapsulate option space containing some options with
  1939. // another option. In this test we create base option that
  1940. // encapsulates option space 'isc' that comprises two other
  1941. // options. Also, for all options their definitions are
  1942. // created.
  1943. TEST_F(Dhcp6ParserTest, optionDataEncapsulate) {
  1944. // @todo DHCP configurations has many dependencies between
  1945. // parameters. First of all, configuration for subnet is
  1946. // inherited from the global values. Thus subnet has to be
  1947. // configured when all global values have been configured.
  1948. // Also, an option can encapsulate another option only
  1949. // if the latter has been configured. For this reason in this
  1950. // test we created two-stage configuration where first we
  1951. // created options that belong to encapsulated option space.
  1952. // In the second stage we add the base option. Also, the Subnet
  1953. // object is configured in the second stage so it is created
  1954. // at the very end (when all other parameters are configured).
  1955. // Starting stage 1. Configure sub-options and their definitions.
  1956. string config = "{ " + genIfaceConfig() + ","
  1957. "\"preferred-lifetime\": 3000,"
  1958. "\"valid-lifetime\": 4000,"
  1959. "\"rebind-timer\": 2000,"
  1960. "\"renew-timer\": 1000,"
  1961. "\"option-data\": [ {"
  1962. " \"name\": \"foo\","
  1963. " \"space\": \"isc\","
  1964. " \"data\": \"1234\""
  1965. " },"
  1966. " {"
  1967. " \"name\": \"foo2\","
  1968. " \"space\": \"isc\","
  1969. " \"data\": \"192.168.2.1\""
  1970. " } ],"
  1971. "\"option-def\": [ {"
  1972. " \"name\": \"foo\","
  1973. " \"code\": 110,"
  1974. " \"type\": \"uint32\","
  1975. " \"space\": \"isc\""
  1976. " },"
  1977. " {"
  1978. " \"name\": \"foo2\","
  1979. " \"code\": 111,"
  1980. " \"type\": \"ipv4-address\","
  1981. " \"space\": \"isc\""
  1982. " } ]"
  1983. "}";
  1984. ConstElementPtr status;
  1985. ElementPtr json = Element::fromJSON(config);
  1986. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1987. ASSERT_TRUE(status);
  1988. checkResult(status, 0);
  1989. CfgMgr::instance().clear();
  1990. // Stage 2. Configure base option and a subnet. Please note that
  1991. // the configuration from the stage 2 is repeated because BIND
  1992. // configuration manager sends whole configuration for the lists
  1993. // where at least one element is being modified or added.
  1994. config = "{ " + genIfaceConfig() + ","
  1995. "\"preferred-lifetime\": 3000,"
  1996. "\"valid-lifetime\": 4000,"
  1997. "\"rebind-timer\": 2000,"
  1998. "\"renew-timer\": 1000,"
  1999. "\"option-data\": [ {"
  2000. " \"name\": \"base-option\","
  2001. " \"data\": \"11\""
  2002. " },"
  2003. " {"
  2004. " \"name\": \"foo\","
  2005. " \"space\": \"isc\","
  2006. " \"data\": \"1234\""
  2007. " },"
  2008. " {"
  2009. " \"name\": \"foo2\","
  2010. " \"space\": \"isc\","
  2011. " \"data\": \"192.168.2.1\""
  2012. " } ],"
  2013. "\"option-def\": [ {"
  2014. " \"name\": \"base-option\","
  2015. " \"code\": 100,"
  2016. " \"type\": \"uint8\","
  2017. " \"space\": \"dhcp6\","
  2018. " \"encapsulate\": \"isc\""
  2019. "},"
  2020. "{"
  2021. " \"name\": \"foo\","
  2022. " \"code\": 110,"
  2023. " \"type\": \"uint32\","
  2024. " \"space\": \"isc\""
  2025. " },"
  2026. " {"
  2027. " \"name\": \"foo2\","
  2028. " \"code\": 111,"
  2029. " \"type\": \"ipv4-address\","
  2030. " \"space\": \"isc\""
  2031. " } ],"
  2032. "\"subnet6\": [ { "
  2033. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2034. " \"subnet\": \"2001:db8:1::/64\""
  2035. " } ]"
  2036. "}";
  2037. json = Element::fromJSON(config);
  2038. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2039. ASSERT_TRUE(status);
  2040. checkResult(status, 0);
  2041. // Get the subnet.
  2042. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2043. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2044. ASSERT_TRUE(subnet);
  2045. // We should have one option available.
  2046. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  2047. ASSERT_TRUE(options);
  2048. ASSERT_EQ(1, options->size());
  2049. // Get the option.
  2050. OptionDescriptor desc = subnet->getCfgOption()->get("dhcp6", 100);
  2051. EXPECT_TRUE(desc.option_);
  2052. EXPECT_EQ(100, desc.option_->getType());
  2053. // This opton should comprise two sub-options.
  2054. // Onf of them is 'foo' with code 110.
  2055. OptionPtr option_foo = desc.option_->getOption(110);
  2056. ASSERT_TRUE(option_foo);
  2057. EXPECT_EQ(110, option_foo->getType());
  2058. // ...another one 'foo2' with code 111.
  2059. OptionPtr option_foo2 = desc.option_->getOption(111);
  2060. ASSERT_TRUE(option_foo2);
  2061. EXPECT_EQ(111, option_foo2->getType());
  2062. }
  2063. // Goal of this test is to verify options configuration
  2064. // for multiple subnets.
  2065. TEST_F(Dhcp6ParserTest, optionDataInMultipleSubnets) {
  2066. ConstElementPtr x;
  2067. string config = "{ " + genIfaceConfig() + ","
  2068. "\"preferred-lifetime\": 3000,"
  2069. "\"rebind-timer\": 2000, "
  2070. "\"renew-timer\": 1000, "
  2071. "\"subnet6\": [ { "
  2072. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2073. " \"subnet\": \"2001:db8:1::/64\", "
  2074. " \"option-data\": [ {"
  2075. " \"name\": \"subscriber-id\","
  2076. " \"data\": \"0102030405060708090A\","
  2077. " \"csv-format\": False"
  2078. " } ]"
  2079. " },"
  2080. " {"
  2081. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  2082. " \"subnet\": \"2001:db8:2::/64\", "
  2083. " \"option-data\": [ {"
  2084. " \"name\": \"user-class\","
  2085. " \"data\": \"FFFEFDFCFB\","
  2086. " \"csv-format\": False"
  2087. " } ]"
  2088. " } ],"
  2089. "\"valid-lifetime\": 4000 }";
  2090. ElementPtr json = Element::fromJSON(config);
  2091. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2092. checkResult(x, 0);
  2093. Subnet6Ptr subnet1 = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2094. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2095. ASSERT_TRUE(subnet1);
  2096. OptionContainerPtr options1 = subnet1->getCfgOption()->getAll("dhcp6");
  2097. ASSERT_EQ(1, options1->size());
  2098. // Get the search index. Index #1 is to search using option code.
  2099. const OptionContainerTypeIndex& idx1 = options1->get<1>();
  2100. // Get the options for specified index. Expecting one option to be
  2101. // returned but in theory we may have multiple options with the same
  2102. // code so we get the range.
  2103. std::pair<OptionContainerTypeIndex::const_iterator,
  2104. OptionContainerTypeIndex::const_iterator> range1 =
  2105. idx1.equal_range(D6O_SUBSCRIBER_ID);
  2106. // Expect single option with the code equal to 38.
  2107. ASSERT_EQ(1, std::distance(range1.first, range1.second));
  2108. const uint8_t subid_expected[] = {
  2109. 0x01, 0x02, 0x03, 0x04, 0x05,
  2110. 0x06, 0x07, 0x08, 0x09, 0x0A
  2111. };
  2112. // Check if option is valid in terms of code and carried data.
  2113. testOption(*range1.first, D6O_SUBSCRIBER_ID, subid_expected,
  2114. sizeof(subid_expected));
  2115. // Test another subnet in the same way.
  2116. Subnet6Ptr subnet2 = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2117. selectSubnet(IOAddress("2001:db8:2::4"), classify_);
  2118. ASSERT_TRUE(subnet2);
  2119. OptionContainerPtr options2 = subnet2->getCfgOption()->getAll("dhcp6");
  2120. ASSERT_EQ(1, options2->size());
  2121. const OptionContainerTypeIndex& idx2 = options2->get<1>();
  2122. std::pair<OptionContainerTypeIndex::const_iterator,
  2123. OptionContainerTypeIndex::const_iterator> range2 =
  2124. idx2.equal_range(D6O_USER_CLASS);
  2125. ASSERT_EQ(1, std::distance(range2.first, range2.second));
  2126. const uint8_t user_class_expected[] = {
  2127. 0xFF, 0xFE, 0xFD, 0xFC, 0xFB
  2128. };
  2129. testOption(*range2.first, D6O_USER_CLASS, user_class_expected,
  2130. sizeof(user_class_expected));
  2131. }
  2132. // The goal of this test is to check that the option carrying a boolean
  2133. // value can be configured using one of the values: "true", "false", "0"
  2134. // or "1".
  2135. TEST_F(Dhcp6ParserTest, optionDataBoolean) {
  2136. // Create configuration. Use standard option 1000.
  2137. std::map<std::string, std::string> params;
  2138. params["name"] = "bool-option";
  2139. params["space"] = "dhcp6";
  2140. params["code"] = "1000";
  2141. params["data"] = "true";
  2142. params["csv-format"] = "true";
  2143. std::string config = createConfigWithOption(params);
  2144. ASSERT_TRUE(executeConfiguration(config, "parse configuration with a"
  2145. " boolean value"));
  2146. // The subnet should now hold one option with the code 1000.
  2147. OptionDescriptor desc =
  2148. getOptionFromSubnet(IOAddress("2001:db8:1::5"), 1000);
  2149. ASSERT_TRUE(desc.option_);
  2150. // This option should be set to "true", represented as 0x1 in the option
  2151. // buffer.
  2152. uint8_t expected_option_data[] = {
  2153. 0x1
  2154. };
  2155. testConfiguration(params, 1000, expected_option_data,
  2156. sizeof(expected_option_data));
  2157. // Configure the option with the "1" value. This should have the same
  2158. // effect as if "true" was specified.
  2159. params["data"] = "1";
  2160. testConfiguration(params, 1000, expected_option_data,
  2161. sizeof(expected_option_data));
  2162. // The value of "1" with a few leading zeros should work too.
  2163. params["data"] = "00001";
  2164. testConfiguration(params, 1000, expected_option_data,
  2165. sizeof(expected_option_data));
  2166. // Configure the option with the "false" value.
  2167. params["data"] = "false";
  2168. // The option buffer should now hold the value of 0.
  2169. expected_option_data[0] = 0;
  2170. testConfiguration(params, 1000, expected_option_data,
  2171. sizeof(expected_option_data));
  2172. // Specifying "0" should have the same effect as "false".
  2173. params["data"] = "0";
  2174. testConfiguration(params, 1000, expected_option_data,
  2175. sizeof(expected_option_data));
  2176. // The same effect should be for multiple 0 chars.
  2177. params["data"] = "00000";
  2178. testConfiguration(params, 1000, expected_option_data,
  2179. sizeof(expected_option_data));
  2180. // Bogus values should not be accepted.
  2181. params["data"] = "bugus";
  2182. testInvalidOptionParam(params);
  2183. params["data"] = "2";
  2184. testInvalidOptionParam(params);
  2185. // Now let's test that it is possible to use binary format.
  2186. params["data"] = "0";
  2187. params["csv-format"] = "false";
  2188. testConfiguration(params, 1000, expected_option_data,
  2189. sizeof(expected_option_data));
  2190. // The binary 1 should work as well.
  2191. params["data"] = "1";
  2192. expected_option_data[0] = 1;
  2193. testConfiguration(params, 1000, expected_option_data,
  2194. sizeof(expected_option_data));
  2195. // As well as an even number of digits.
  2196. params["data"] = "01";
  2197. testConfiguration(params, 1000, expected_option_data,
  2198. sizeof(expected_option_data));
  2199. }
  2200. // Verify that empty option name is rejected in the configuration.
  2201. TEST_F(Dhcp6ParserTest, optionNameEmpty) {
  2202. // Empty option names not allowed.
  2203. testInvalidOptionParam("", "name");
  2204. }
  2205. // Verify that empty option name with spaces is rejected
  2206. // in the configuration.
  2207. TEST_F(Dhcp6ParserTest, optionNameSpaces) {
  2208. // Spaces in option names not allowed.
  2209. testInvalidOptionParam("option foo", "name");
  2210. }
  2211. // Verify that negative option code is rejected in the configuration.
  2212. TEST_F(Dhcp6ParserTest, optionCodeNegative) {
  2213. // Check negative option code -4. This should fail too.
  2214. testInvalidOptionParam("-4", "code");
  2215. }
  2216. // Verify that out of bounds option code is rejected in the configuration.
  2217. TEST_F(Dhcp6ParserTest, optionCodeNonUint16) {
  2218. // The valid option codes are uint16_t values so passing
  2219. // uint16_t maximum value incremented by 1 should result
  2220. // in failure.
  2221. testInvalidOptionParam("65536", "code");
  2222. }
  2223. // Verify that out of bounds option code is rejected in the configuration.
  2224. TEST_F(Dhcp6ParserTest, optionCodeHighNonUint16) {
  2225. // Another check for uint16_t overflow but this time
  2226. // let's pass even greater option code value.
  2227. testInvalidOptionParam("70000", "code");
  2228. }
  2229. // Verify that zero option code is rejected in the configuration.
  2230. TEST_F(Dhcp6ParserTest, optionCodeZero) {
  2231. // Option code 0 is reserved and should not be accepted
  2232. // by configuration parser.
  2233. testInvalidOptionParam("0", "code");
  2234. }
  2235. // Verify that option data which contains non hexadecimal characters
  2236. // is rejected by the configuration.
  2237. TEST_F(Dhcp6ParserTest, optionDataInvalidChar) {
  2238. // Option code 0 is reserved and should not be accepted
  2239. // by configuration parser.
  2240. testInvalidOptionParam("01020R", "data");
  2241. }
  2242. // Verify that option data containing '0x' prefix is rejected
  2243. // by the configuration.
  2244. TEST_F(Dhcp6ParserTest, optionDataUnexpectedPrefix) {
  2245. // Option code 0 is reserved and should not be accepted
  2246. // by configuration parser.
  2247. testInvalidOptionParam("0x0102", "data");
  2248. }
  2249. // Verify that either lower or upper case characters are allowed
  2250. // to specify the option data.
  2251. TEST_F(Dhcp6ParserTest, optionDataLowerCase) {
  2252. ConstElementPtr x;
  2253. std::string config = createConfigWithOption("0a0b0C0D", "data");
  2254. ElementPtr json = Element::fromJSON(config);
  2255. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2256. checkResult(x, 0);
  2257. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2258. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2259. ASSERT_TRUE(subnet);
  2260. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  2261. ASSERT_EQ(1, options->size());
  2262. // Get the search index. Index #1 is to search using option code.
  2263. const OptionContainerTypeIndex& idx = options->get<1>();
  2264. // Get the options for specified index. Expecting one option to be
  2265. // returned but in theory we may have multiple options with the same
  2266. // code so we get the range.
  2267. std::pair<OptionContainerTypeIndex::const_iterator,
  2268. OptionContainerTypeIndex::const_iterator> range =
  2269. idx.equal_range(D6O_SUBSCRIBER_ID);
  2270. // Expect single option with the code equal to 38.
  2271. ASSERT_EQ(1, std::distance(range.first, range.second));
  2272. const uint8_t subid_expected[] = {
  2273. 0x0A, 0x0B, 0x0C, 0x0D
  2274. };
  2275. // Check if option is valid in terms of code and carried data.
  2276. testOption(*range.first, D6O_SUBSCRIBER_ID, subid_expected,
  2277. sizeof(subid_expected));
  2278. }
  2279. // Verify that specific option object is returned for standard
  2280. // option which has dedicated option class derived from Option.
  2281. TEST_F(Dhcp6ParserTest, stdOptionData) {
  2282. ConstElementPtr x;
  2283. std::map<std::string, std::string> params;
  2284. params["name"] = "ia-na";
  2285. params["space"] = "dhcp6";
  2286. // Option code 3 means OPTION_IA_NA.
  2287. params["code"] = "3";
  2288. params["data"] = "12345, 6789, 1516";
  2289. params["csv-format"] = "True";
  2290. std::string config = createConfigWithOption(params);
  2291. ElementPtr json = Element::fromJSON(config);
  2292. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2293. checkResult(x, 0);
  2294. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2295. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2296. ASSERT_TRUE(subnet);
  2297. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  2298. ASSERT_EQ(1, options->size());
  2299. // Get the search index. Index #1 is to search using option code.
  2300. const OptionContainerTypeIndex& idx = options->get<1>();
  2301. // Get the options for specified index. Expecting one option to be
  2302. // returned but in theory we may have multiple options with the same
  2303. // code so we get the range.
  2304. std::pair<OptionContainerTypeIndex::const_iterator,
  2305. OptionContainerTypeIndex::const_iterator> range =
  2306. idx.equal_range(D6O_IA_NA);
  2307. // Expect single option with the code equal to IA_NA option code.
  2308. ASSERT_EQ(1, std::distance(range.first, range.second));
  2309. // The actual pointer to the option is held in the option field
  2310. // in the structure returned.
  2311. OptionPtr option = range.first->option_;
  2312. ASSERT_TRUE(option);
  2313. // Option object returned for here is expected to be Option6IA
  2314. // which is derived from Option. This class is dedicated to
  2315. // represent standard option IA_NA.
  2316. boost::shared_ptr<Option6IA> optionIA =
  2317. boost::dynamic_pointer_cast<Option6IA>(option);
  2318. // If cast is unsuccessful than option returned was of a
  2319. // different type than Option6IA. This is wrong.
  2320. ASSERT_TRUE(optionIA);
  2321. // If cast was successful we may use accessors exposed by
  2322. // Option6IA to validate that the content of this option
  2323. // has been set correctly.
  2324. EXPECT_EQ(12345, optionIA->getIAID());
  2325. EXPECT_EQ(6789, optionIA->getT1());
  2326. EXPECT_EQ(1516, optionIA->getT2());
  2327. }
  2328. // This test checks if vendor options can be specified in the config file
  2329. // (in hex format), and later retrieved from configured subnet
  2330. TEST_F(Dhcp6ParserTest, vendorOptionsHex) {
  2331. // This configuration string is to configure two options
  2332. // sharing the code 1 and belonging to the different vendor spaces.
  2333. // (different vendor-id values).
  2334. string config = "{ " + genIfaceConfig() + ","
  2335. "\"preferred-lifetime\": 3000,"
  2336. "\"valid-lifetime\": 4000,"
  2337. "\"rebind-timer\": 2000,"
  2338. "\"renew-timer\": 1000,"
  2339. "\"option-data\": [ {"
  2340. " \"name\": \"option-one\","
  2341. " \"space\": \"vendor-4491\","
  2342. " \"code\": 100,"
  2343. " \"data\": \"ABCDEF0105\","
  2344. " \"csv-format\": False"
  2345. " },"
  2346. " {"
  2347. " \"name\": \"option-two\","
  2348. " \"space\": \"vendor-1234\","
  2349. " \"code\": 100,"
  2350. " \"data\": \"1234\","
  2351. " \"csv-format\": False"
  2352. " } ],"
  2353. "\"subnet6\": [ { "
  2354. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2355. " \"subnet\": \"2001:db8:1::/64\""
  2356. " } ]"
  2357. "}";
  2358. ConstElementPtr status;
  2359. ElementPtr json = Element::fromJSON(config);
  2360. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2361. ASSERT_TRUE(status);
  2362. checkResult(status, 0);
  2363. // Options should be now available for the subnet.
  2364. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2365. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2366. ASSERT_TRUE(subnet);
  2367. // Try to get the option from the vendor space 4491
  2368. OptionDescriptor desc1 = subnet->getCfgOption()->get(4491, 100);
  2369. ASSERT_TRUE(desc1.option_);
  2370. EXPECT_EQ(100, desc1.option_->getType());
  2371. // Try to get the option from the vendor space 1234
  2372. OptionDescriptor desc2 = subnet->getCfgOption()->get(1234, 100);
  2373. ASSERT_TRUE(desc2.option_);
  2374. EXPECT_EQ(100, desc1.option_->getType());
  2375. // Try to get the non-existing option from the non-existing
  2376. // option space and expect that option is not returned.
  2377. OptionDescriptor desc3 = subnet->getCfgOption()->get(5678, 38);
  2378. ASSERT_FALSE(desc3.option_);
  2379. }
  2380. // This test checks if vendor options can be specified in the config file,
  2381. // (in csv format), and later retrieved from configured subnet
  2382. TEST_F(Dhcp6ParserTest, vendorOptionsCsv) {
  2383. // This configuration string is to configure two options
  2384. // sharing the code 1 and belonging to the different vendor spaces.
  2385. // (different vendor-id values).
  2386. string config = "{ " + genIfaceConfig() + ","
  2387. "\"preferred-lifetime\": 3000,"
  2388. "\"valid-lifetime\": 4000,"
  2389. "\"rebind-timer\": 2000,"
  2390. "\"renew-timer\": 1000,"
  2391. "\"option-data\": [ {"
  2392. " \"name\": \"foo\","
  2393. " \"space\": \"vendor-4491\","
  2394. " \"code\": 100,"
  2395. " \"data\": \"this is a string vendor-opt\""
  2396. " } ],"
  2397. "\"option-def\": [ {"
  2398. " \"name\": \"foo\","
  2399. " \"code\": 100,"
  2400. " \"type\": \"string\","
  2401. " \"space\": \"vendor-4491\""
  2402. " } ],"
  2403. "\"subnet6\": [ { "
  2404. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2405. " \"subnet\": \"2001:db8:1::/64\""
  2406. " } ]"
  2407. "}";
  2408. ConstElementPtr status;
  2409. ElementPtr json = Element::fromJSON(config);
  2410. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2411. ASSERT_TRUE(status);
  2412. checkResult(status, 0);
  2413. // Options should be now available for the subnet.
  2414. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2415. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2416. ASSERT_TRUE(subnet);
  2417. // Try to get the option from the vendor space 4491
  2418. OptionDescriptor desc1 = subnet->getCfgOption()->get(4491, 100);
  2419. ASSERT_TRUE(desc1.option_);
  2420. EXPECT_EQ(100, desc1.option_->getType());
  2421. // Try to get the non-existing option from the non-existing
  2422. // option space and expect that option is not returned.
  2423. OptionDescriptor desc2 = subnet->getCfgOption()->get(5678, 100);
  2424. ASSERT_FALSE(desc2.option_);
  2425. }
  2426. /// @todo add tests similar to vendorOptionsCsv and vendorOptionsHex, but for
  2427. /// vendor options defined in a subnet.
  2428. // The goal of this test is to verify that the standard option can
  2429. // be configured to encapsulate multiple other options.
  2430. /// @todo This test is currently disabled because it relies on the option
  2431. /// 17 which is treated differently than all other options. There are no
  2432. /// other standard options used by Kea which would encapsulate other
  2433. /// options and for which values could be configured here.
  2434. TEST_F(Dhcp6ParserTest, DISABLED_stdOptionDataEncapsulate) {
  2435. // The configuration is two stage process in this test.
  2436. // In the first stage we create definitions of suboptions
  2437. // that we will add to the base option.
  2438. // Let's create some dummy options: foo and foo2.
  2439. string config = "{ " + genIfaceConfig() + ","
  2440. "\"preferred-lifetime\": 3000,"
  2441. "\"valid-lifetime\": 4000,"
  2442. "\"rebind-timer\": 2000,"
  2443. "\"renew-timer\": 1000,"
  2444. "\"option-data\": [ {"
  2445. " \"name\": \"foo\","
  2446. " \"space\": \"vendor-opts-space\","
  2447. " \"data\": \"1234\""
  2448. " },"
  2449. " {"
  2450. " \"name\": \"foo2\","
  2451. " \"space\": \"vendor-opts-space\","
  2452. " \"data\": \"192.168.2.1\""
  2453. " } ],"
  2454. "\"option-def\": [ {"
  2455. " \"name\": \"foo\","
  2456. " \"code\": 110,"
  2457. " \"type\": \"uint32\","
  2458. " \"space\": \"vendor-opts-space\""
  2459. " },"
  2460. " {"
  2461. " \"name\": \"foo2\","
  2462. " \"code\": 111,"
  2463. " \"type\": \"ipv4-address\","
  2464. " \"space\": \"vendor-opts-space\""
  2465. " } ]"
  2466. "}";
  2467. ConstElementPtr status;
  2468. ElementPtr json = Element::fromJSON(config);
  2469. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2470. ASSERT_TRUE(status);
  2471. checkResult(status, 0);
  2472. CfgMgr::instance().clear();
  2473. // Once the definitions have been added we can configure the
  2474. // standard option #17. This option comprises an enterprise
  2475. // number and sub options. By convention (introduced in
  2476. // std_option_defs.h) option named 'vendor-opts'
  2477. // encapsulates the option space named 'vendor-opts-space'.
  2478. // We add our dummy options to this option space and thus
  2479. // they should be included as sub-options in the 'vendor-opts'
  2480. // option.
  2481. config = "{ " + genIfaceConfig() + ","
  2482. "\"rebind-timer\": 2000,"
  2483. "\"renew-timer\": 1000,"
  2484. "\"option-data\": [ {"
  2485. " \"name\": \"vendor-opts\","
  2486. " \"data\": \"1234\""
  2487. " },"
  2488. " {"
  2489. " \"name\": \"foo\","
  2490. " \"space\": \"vendor-opts-space\","
  2491. " \"data\": \"1234\""
  2492. " },"
  2493. " {"
  2494. " \"name\": \"foo2\","
  2495. " \"space\": \"vendor-opts-space\","
  2496. " \"data\": \"192.168.2.1\""
  2497. " } ],"
  2498. "\"option-def\": [ {"
  2499. " \"name\": \"foo\","
  2500. " \"code\": 110,"
  2501. " \"type\": \"uint32\","
  2502. " \"space\": \"vendor-opts-space\""
  2503. " },"
  2504. " {"
  2505. " \"name\": \"foo2\","
  2506. " \"code\": 111,"
  2507. " \"type\": \"ipv4-address\","
  2508. " \"space\": \"vendor-opts-space\""
  2509. " } ],"
  2510. "\"subnet6\": [ { "
  2511. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2512. " \"subnet\": \"2001:db8:1::/64\""
  2513. " } ]"
  2514. "}";
  2515. json = Element::fromJSON(config);
  2516. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2517. ASSERT_TRUE(status);
  2518. checkResult(status, 0);
  2519. // Get the subnet.
  2520. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2521. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2522. ASSERT_TRUE(subnet);
  2523. // We should have one option available.
  2524. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  2525. ASSERT_TRUE(options);
  2526. ASSERT_EQ(1, options->size());
  2527. // Get the option.
  2528. OptionDescriptor desc = subnet->getCfgOption()->get("dhcp6", D6O_VENDOR_OPTS);
  2529. EXPECT_TRUE(desc.option_);
  2530. EXPECT_EQ(D6O_VENDOR_OPTS, desc.option_->getType());
  2531. // Option with the code 110 should be added as a sub-option.
  2532. OptionPtr option_foo = desc.option_->getOption(110);
  2533. ASSERT_TRUE(option_foo);
  2534. EXPECT_EQ(110, option_foo->getType());
  2535. // This option comprises a single uint32_t value thus it is
  2536. // represented by OptionInt<uint32_t> class. Let's get the
  2537. // object of this type.
  2538. boost::shared_ptr<OptionInt<uint32_t> > option_foo_uint32 =
  2539. boost::dynamic_pointer_cast<OptionInt<uint32_t> >(option_foo);
  2540. ASSERT_TRUE(option_foo_uint32);
  2541. // Validate the value according to the configuration.
  2542. EXPECT_EQ(1234, option_foo_uint32->getValue());
  2543. // Option with the code 111 should be added as a sub-option.
  2544. OptionPtr option_foo2 = desc.option_->getOption(111);
  2545. ASSERT_TRUE(option_foo2);
  2546. EXPECT_EQ(111, option_foo2->getType());
  2547. // This option comprises the IPV4 address. Such option is
  2548. // represented by OptionCustom object.
  2549. OptionCustomPtr option_foo2_v4 =
  2550. boost::dynamic_pointer_cast<OptionCustom>(option_foo2);
  2551. ASSERT_TRUE(option_foo2_v4);
  2552. // Get the IP address carried by this option and validate it.
  2553. EXPECT_EQ("192.168.2.1", option_foo2_v4->readAddress().toText());
  2554. // Option with the code 112 should not be added.
  2555. EXPECT_FALSE(desc.option_->getOption(112));
  2556. }
  2557. // Tests of the hooks libraries configuration. All tests have the pre-
  2558. // condition (checked in the test fixture's SetUp() method) that no hooks
  2559. // libraries are loaded at the start of the tests.
  2560. // Helper function to return a configuration containing an arbitrary number
  2561. // of hooks libraries.
  2562. std::string
  2563. buildHooksLibrariesConfig(const std::vector<std::string>& libraries) {
  2564. const string lbrace("{");
  2565. const string rbrace("}");
  2566. const string liblabel("\"library\": ");
  2567. const string quote("\"");
  2568. // Create the first part of the configuration string.
  2569. string config =
  2570. "{ \"interfaces-config\": { },"
  2571. "\"hooks-libraries\": [";
  2572. // Append the libraries (separated by commas if needed)
  2573. for (int i = 0; i < libraries.size(); ++i) {
  2574. if (i > 0) {
  2575. config += string(", ");
  2576. }
  2577. config += (lbrace + liblabel + quote + libraries[i] + quote + rbrace);
  2578. }
  2579. // Append the remainder of the configuration.
  2580. config += string(
  2581. "],"
  2582. "\"rebind-timer\": 2000,"
  2583. "\"renew-timer\": 1000,"
  2584. "\"option-data\": [ {"
  2585. " \"name\": \"foo\","
  2586. " \"space\": \"vendor-opts-space\","
  2587. " \"data\": \"1234\""
  2588. " },"
  2589. " {"
  2590. " \"name\": \"foo2\","
  2591. " \"space\": \"vendor-opts-space\","
  2592. " \"data\": \"192.168.2.1\""
  2593. " } ],"
  2594. "\"option-def\": [ {"
  2595. " \"name\": \"foo\","
  2596. " \"code\": 110,"
  2597. " \"type\": \"uint32\","
  2598. " \"space\": \"vendor-opts-space\""
  2599. " },"
  2600. " {"
  2601. " \"name\": \"foo2\","
  2602. " \"code\": 111,"
  2603. " \"type\": \"ipv4-address\","
  2604. " \"space\": \"vendor-opts-space\""
  2605. " } ]"
  2606. "}");
  2607. return (config);
  2608. }
  2609. // Convenience function for creating hooks library configuration with one or
  2610. // two character string constants.
  2611. std::string
  2612. buildHooksLibrariesConfig(const char* library1 = NULL,
  2613. const char* library2 = NULL) {
  2614. std::vector<std::string> libraries;
  2615. if (library1 != NULL) {
  2616. libraries.push_back(string(library1));
  2617. if (library2 != NULL) {
  2618. libraries.push_back(string(library2));
  2619. }
  2620. }
  2621. return (buildHooksLibrariesConfig(libraries));
  2622. }
  2623. // The goal of this test is to verify the configuration of hooks libraries if
  2624. // none are specified.
  2625. TEST_F(Dhcp6ParserTest, NoHooksLibraries) {
  2626. // Parse a configuration containing no names.
  2627. string config = buildHooksLibrariesConfig();
  2628. if (!executeConfiguration(config,
  2629. "set configuration with no hooks libraries")) {
  2630. FAIL() << "Unable to execute configuration";
  2631. } else {
  2632. // No libraries should be loaded at the end of the test.
  2633. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  2634. EXPECT_TRUE(libraries.empty());
  2635. }
  2636. }
  2637. // Verify parsing fails with one library that will fail validation.
  2638. TEST_F(Dhcp6ParserTest, InvalidLibrary) {
  2639. // Parse a configuration containing a failing library.
  2640. string config = buildHooksLibrariesConfig(NOT_PRESENT_LIBRARY);
  2641. ConstElementPtr status;
  2642. ElementPtr json = Element::fromJSON(config);
  2643. ASSERT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2644. // The status object must not be NULL
  2645. ASSERT_TRUE(status);
  2646. // Returned value should not be 0
  2647. comment_ = parseAnswer(rcode_, status);
  2648. EXPECT_NE(0, rcode_);
  2649. }
  2650. // Verify the configuration of hooks libraries with two being specified.
  2651. TEST_F(Dhcp6ParserTest, LibrariesSpecified) {
  2652. // Marker files should not be present.
  2653. EXPECT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE));
  2654. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  2655. // Set up the configuration with two libraries and load them.
  2656. string config = buildHooksLibrariesConfig(CALLOUT_LIBRARY_1,
  2657. CALLOUT_LIBRARY_2);
  2658. ASSERT_TRUE(executeConfiguration(config,
  2659. "load two valid libraries"));
  2660. // Expect two libraries to be loaded in the correct order (load marker file
  2661. // is present, no unload marker file).
  2662. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  2663. ASSERT_EQ(2, libraries.size());
  2664. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  2665. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  2666. CfgMgr::instance().commit();
  2667. // Unload the libraries. The load file should not have changed, but
  2668. // the unload one should indicate the unload() functions have been run.
  2669. config = buildHooksLibrariesConfig();
  2670. ASSERT_TRUE(executeConfiguration(config, "unloading libraries"));
  2671. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  2672. EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "21"));
  2673. // Expect the hooks system to say that none are loaded.
  2674. libraries = HooksManager::getLibraryNames();
  2675. EXPECT_TRUE(libraries.empty());
  2676. }
  2677. // This test verifies that it is possible to select subset of interfaces on
  2678. // which server should listen.
  2679. TEST_F(Dhcp6ParserTest, selectedInterfaces) {
  2680. IfaceMgrTestConfig test_config(true);
  2681. // Make sure there is no garbage interface configuration in the CfgMgr.
  2682. ASSERT_FALSE(test_config.socketOpen("eth0", AF_INET6));
  2683. ASSERT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  2684. ConstElementPtr status;
  2685. string config = "{ \"interfaces-config\": {"
  2686. " \"interfaces\": [ \"eth0\" ]"
  2687. "},"
  2688. "\"preferred-lifetime\": 3000,"
  2689. "\"rebind-timer\": 2000, "
  2690. "\"renew-timer\": 1000, "
  2691. "\"valid-lifetime\": 4000 }";
  2692. ElementPtr json = Element::fromJSON(config);
  2693. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2694. // returned value must be 1 (values error)
  2695. // as the pool does not belong to that subnet
  2696. checkResult(status, 0);
  2697. CfgMgr::instance().getStagingCfg()->getCfgIface()->openSockets(AF_INET6, 10000);
  2698. // eth0 and eth1 were explicitly selected. eth2 was not.
  2699. EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET6));
  2700. EXPECT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  2701. }
  2702. // This test verifies that it is possible to configure the server to listen on
  2703. // all interfaces.
  2704. TEST_F(Dhcp6ParserTest, allInterfaces) {
  2705. IfaceMgrTestConfig test_config(true);
  2706. ASSERT_FALSE(test_config.socketOpen("eth0", AF_INET6));
  2707. ASSERT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  2708. ConstElementPtr status;
  2709. // This configuration specifies two interfaces on which server should listen
  2710. // but also includes '*'. This keyword switches server into the
  2711. // mode when it listens on all interfaces regardless of what interface names
  2712. // were specified in the "interfaces" parameter.
  2713. string config = "{ \"interfaces-config\": {"
  2714. " \"interfaces\": [ \"eth0\", \"eth1\", \"*\" ]"
  2715. "},"
  2716. "\"preferred-lifetime\": 3000,"
  2717. "\"rebind-timer\": 2000, "
  2718. "\"renew-timer\": 1000, "
  2719. "\"valid-lifetime\": 4000 }";
  2720. ElementPtr json = Element::fromJSON(config);
  2721. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2722. checkResult(status, 0);
  2723. CfgMgr::instance().getStagingCfg()->getCfgIface()->openSockets(AF_INET6, 10000);
  2724. // All interfaces should be now active.
  2725. EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET6));
  2726. EXPECT_TRUE(test_config.socketOpen("eth1", AF_INET6));
  2727. }
  2728. // This test checks if it is possible to specify relay information
  2729. TEST_F(Dhcp6ParserTest, subnetRelayInfo) {
  2730. ConstElementPtr status;
  2731. // A config with relay information.
  2732. string config = "{ " + genIfaceConfig() + ","
  2733. "\"rebind-timer\": 2000, "
  2734. "\"renew-timer\": 1000, "
  2735. "\"subnet6\": [ { "
  2736. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  2737. " \"relay\": { "
  2738. " \"ip-address\": \"2001:db8:1::abcd\""
  2739. " },"
  2740. " \"subnet\": \"2001:db8:1::/64\" } ],"
  2741. "\"preferred-lifetime\": 3000, "
  2742. "\"valid-lifetime\": 4000 }";
  2743. ElementPtr json = Element::fromJSON(config);
  2744. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2745. // returned value should be 0 (configuration success)
  2746. checkResult(status, 0);
  2747. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2748. selectSubnet(IOAddress("2001:db8:1::1"), classify_);
  2749. ASSERT_TRUE(subnet);
  2750. EXPECT_EQ("2001:db8:1::abcd", subnet->getRelayInfo().addr_.toText());
  2751. }
  2752. // Goal of this test is to verify that multiple subnets can be configured
  2753. // with defined client classes.
  2754. TEST_F(Dhcp6ParserTest, classifySubnets) {
  2755. ConstElementPtr x;
  2756. string config = "{ " + genIfaceConfig() + ","
  2757. "\"preferred-lifetime\": 3000,"
  2758. "\"rebind-timer\": 2000, "
  2759. "\"renew-timer\": 1000, "
  2760. "\"subnet6\": [ { "
  2761. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2762. " \"subnet\": \"2001:db8:1::/64\", "
  2763. " \"client-class\": \"alpha\" "
  2764. " },"
  2765. " {"
  2766. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  2767. " \"subnet\": \"2001:db8:2::/64\", "
  2768. " \"client-class\": \"beta\" "
  2769. " },"
  2770. " {"
  2771. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  2772. " \"subnet\": \"2001:db8:3::/64\", "
  2773. " \"client-class\": \"gamma\" "
  2774. " },"
  2775. " {"
  2776. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  2777. " \"subnet\": \"2001:db8:4::/64\" "
  2778. " } ],"
  2779. "\"valid-lifetime\": 4000 }";
  2780. ElementPtr json = Element::fromJSON(config);
  2781. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2782. checkResult(x, 0);
  2783. const Subnet6Collection* subnets =
  2784. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  2785. ASSERT_TRUE(subnets);
  2786. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  2787. // Let's check if client belonging to alpha class is supported in subnet[0]
  2788. // and not supported in any other subnet (except subnet[3], which allows
  2789. // everyone).
  2790. ClientClasses classes;
  2791. classes.insert("alpha");
  2792. EXPECT_TRUE (subnets->at(0)->clientSupported(classes));
  2793. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2794. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2795. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2796. // Let's check if client belonging to beta class is supported in subnet[1]
  2797. // and not supported in any other subnet (except subnet[3], which allows
  2798. // everyone).
  2799. classes.clear();
  2800. classes.insert("beta");
  2801. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2802. EXPECT_TRUE (subnets->at(1)->clientSupported(classes));
  2803. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2804. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2805. // Let's check if client belonging to gamma class is supported in subnet[2]
  2806. // and not supported in any other subnet (except subnet[3], which allows
  2807. // everyone).
  2808. classes.clear();
  2809. classes.insert("gamma");
  2810. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2811. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2812. EXPECT_TRUE (subnets->at(2)->clientSupported(classes));
  2813. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2814. // Let's check if client belonging to some other class (not mentioned in
  2815. // the config) is supported only in subnet[3], which allows everyone.
  2816. classes.clear();
  2817. classes.insert("delta");
  2818. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2819. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2820. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2821. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2822. // Finally, let's check class-less client. He should be allowed only in
  2823. // the last subnet, which does not have any class restrictions.
  2824. classes.clear();
  2825. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2826. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2827. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2828. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2829. }
  2830. // This test checks the ability of the server to parse a configuration
  2831. // containing a full, valid dhcp-ddns (D2ClientConfig) entry.
  2832. TEST_F(Dhcp6ParserTest, d2ClientConfig) {
  2833. ConstElementPtr status;
  2834. // Verify that the D2 configuraiton can be fetched and is set to disabled.
  2835. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2836. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  2837. // Verify that the convenience method agrees.
  2838. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  2839. string config_str = "{ " + genIfaceConfig() + ","
  2840. "\"preferred-lifetime\": 3000,"
  2841. "\"valid-lifetime\": 4000,"
  2842. "\"rebind-timer\": 2000, "
  2843. "\"renew-timer\": 1000, "
  2844. "\"subnet6\": [ { "
  2845. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2846. " \"subnet\": \"2001:db8:1::/64\" } ], "
  2847. " \"dhcp-ddns\" : {"
  2848. " \"enable-updates\" : true, "
  2849. " \"server-ip\" : \"3001::1\", "
  2850. " \"server-port\" : 777, "
  2851. " \"sender-ip\" : \"3001::2\", "
  2852. " \"sender-port\" : 778, "
  2853. " \"max-queue-size\" : 2048, "
  2854. " \"ncr-protocol\" : \"UDP\", "
  2855. " \"ncr-format\" : \"JSON\", "
  2856. " \"always-include-fqdn\" : true, "
  2857. " \"allow-client-update\" : true, "
  2858. " \"override-no-update\" : true, "
  2859. " \"override-client-update\" : true, "
  2860. " \"replace-client-name\" : true, "
  2861. " \"generated-prefix\" : \"test.prefix\", "
  2862. " \"qualifying-suffix\" : \"test.suffix.\" },"
  2863. "\"valid-lifetime\": 4000 }";
  2864. // Convert the JSON string to configuration elements.
  2865. ElementPtr config;
  2866. ASSERT_NO_THROW(config = Element::fromJSON(config_str));
  2867. // Pass the configuration in for parsing.
  2868. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config));
  2869. // check if returned status is OK
  2870. checkResult(status, 0);
  2871. // Verify that DHCP-DDNS updating is enabled.
  2872. EXPECT_TRUE(CfgMgr::instance().ddnsEnabled());
  2873. // Verify that the D2 configuration can be retrieved.
  2874. d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2875. ASSERT_TRUE(d2_client_config);
  2876. // Verify that the configuration values are correct.
  2877. EXPECT_TRUE(d2_client_config->getEnableUpdates());
  2878. EXPECT_EQ("3001::1", d2_client_config->getServerIp().toText());
  2879. EXPECT_EQ(777, d2_client_config->getServerPort());
  2880. EXPECT_EQ("3001::2", d2_client_config->getSenderIp().toText());
  2881. EXPECT_EQ(778, d2_client_config->getSenderPort());
  2882. EXPECT_EQ(2048, d2_client_config->getMaxQueueSize());
  2883. EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_client_config->getNcrProtocol());
  2884. EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_client_config->getNcrFormat());
  2885. EXPECT_TRUE(d2_client_config->getAlwaysIncludeFqdn());
  2886. EXPECT_TRUE(d2_client_config->getOverrideNoUpdate());
  2887. EXPECT_TRUE(d2_client_config->getOverrideClientUpdate());
  2888. EXPECT_TRUE(d2_client_config->getReplaceClientName());
  2889. EXPECT_EQ("test.prefix", d2_client_config->getGeneratedPrefix());
  2890. EXPECT_EQ("test.suffix.", d2_client_config->getQualifyingSuffix());
  2891. }
  2892. // This test checks the ability of the server to handle a configuration
  2893. // containing an invalid dhcp-ddns (D2ClientConfig) entry.
  2894. TEST_F(Dhcp6ParserTest, invalidD2ClientConfig) {
  2895. ConstElementPtr status;
  2896. // Configuration string with an invalid D2 client config,
  2897. // "server-ip" is invalid.
  2898. string config_str = "{ " + genIfaceConfig() + ","
  2899. "\"rebind-timer\": 2000, "
  2900. "\"renew-timer\": 1000, "
  2901. "\"subnet6\": [ { "
  2902. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2903. " \"subnet\": \"2001:db8:1::/64\" } ], "
  2904. " \"dhcp-ddns\" : {"
  2905. " \"enable-updates\" : true, "
  2906. " \"server-ip\" : \"bogus-value\", "
  2907. " \"server-port\" : 5301, "
  2908. " \"ncr-protocol\" : \"UDP\", "
  2909. " \"ncr-format\" : \"JSON\", "
  2910. " \"always-include-fqdn\" : true, "
  2911. " \"allow-client-update\" : true, "
  2912. " \"override-no-update\" : true, "
  2913. " \"override-client-update\" : true, "
  2914. " \"replace-client-name\" : true, "
  2915. " \"generated-prefix\" : \"test.prefix\", "
  2916. " \"qualifying-suffix\" : \"test.suffix.\" },"
  2917. "\"valid-lifetime\": 4000 }";
  2918. // Convert the JSON string to configuration elements.
  2919. ElementPtr config;
  2920. ASSERT_NO_THROW(config = Element::fromJSON(config_str));
  2921. // Configuration should not throw, but should fail.
  2922. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config));
  2923. // check if returned status is failed.
  2924. checkResult(status, 1);
  2925. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  2926. // Verify that the D2 configuraiton can be fetched and is set to disabled.
  2927. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2928. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  2929. // Verify that the convenience method agrees.
  2930. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  2931. }
  2932. /// @brief Checks if the reservation is in the range of reservations.
  2933. ///
  2934. /// @param resrv Reservation to be searched for.
  2935. /// @param range Range of reservations returned by the @c Host object
  2936. /// in which the reservation will be searched.
  2937. bool reservationExists(const IPv6Resrv& resrv, const IPv6ResrvRange& range) {
  2938. for (IPv6ResrvIterator it = range.first; it != range.second;
  2939. ++it) {
  2940. if (resrv == it->second) {
  2941. return (true);
  2942. }
  2943. }
  2944. return (false);
  2945. }
  2946. // This test verifies that the host reservations can be specified for
  2947. // respective IPv6 subnets.
  2948. TEST_F(Dhcp6ParserTest, reservations) {
  2949. ConstElementPtr x;
  2950. string config = "{ " + genIfaceConfig() + ","
  2951. "\"rebind-timer\": 2000, "
  2952. "\"renew-timer\": 1000, "
  2953. "\"subnet6\": [ "
  2954. " { "
  2955. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2956. " \"subnet\": \"2001:db8:1::/64\", "
  2957. " \"id\": 123,"
  2958. " \"reservations\": ["
  2959. " ]"
  2960. " },"
  2961. " {"
  2962. " \"reservations\": ["
  2963. " {"
  2964. " \"duid\": \"01:02:03:04:05:06:07:08:09:0A\","
  2965. " \"ip-addresses\": [ \"2001:db8:2::1234\" ],"
  2966. " \"hostname\": \"\""
  2967. " },"
  2968. " {"
  2969. " \"hw-address\": \"01:02:03:04:05:06\","
  2970. " \"ip-addresses\": [ \"2001:db8:2::abcd\" ],"
  2971. " \"hostname\": \"\""
  2972. " }"
  2973. " ],"
  2974. " \"pools\": [ ],"
  2975. " \"subnet\": \"2001:db8:2::/64\", "
  2976. " \"id\": 234"
  2977. " },"
  2978. " {"
  2979. " \"pools\": [ ],"
  2980. " \"subnet\": \"2001:db8:3::/64\", "
  2981. " \"id\": 542,"
  2982. " \"reservations\": ["
  2983. " {"
  2984. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\","
  2985. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  2986. " \"hostname\": \"\""
  2987. " },"
  2988. " {"
  2989. " \"hw-address\": \"06:05:04:03:02:01\","
  2990. " \"prefixes\": [ \"2001:db8:3:1::/96\" ],"
  2991. " \"hostname\": \"\""
  2992. " }"
  2993. " ]"
  2994. " } "
  2995. "], "
  2996. "\"preferred-lifetime\": 3000,"
  2997. "\"valid-lifetime\": 4000 }";
  2998. ElementPtr json = Element::fromJSON(config);
  2999. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3000. checkResult(x, 0);
  3001. // Make sure all subnets have been successfully configured. There is no
  3002. // need to sanity check the subnet properties because it should have
  3003. // been already tested by other tests.
  3004. const Subnet6Collection* subnets =
  3005. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  3006. ASSERT_TRUE(subnets);
  3007. ASSERT_EQ(3, subnets->size());
  3008. // Hosts configuration must be available.
  3009. CfgHostsPtr hosts_cfg = CfgMgr::instance().getStagingCfg()->getCfgHosts();
  3010. ASSERT_TRUE(hosts_cfg);
  3011. // Let's create an object holding hardware address of the host having
  3012. // a reservation in the subnet having id of 234. For simplicity the
  3013. // address is a collection of numbers from 1 to 6.
  3014. std::vector<uint8_t> hwaddr_vec;
  3015. for (int i = 1; i < 7; ++i) {
  3016. hwaddr_vec.push_back(static_cast<uint8_t>(i));
  3017. }
  3018. HWAddrPtr hwaddr(new HWAddr(hwaddr_vec, HTYPE_ETHER));
  3019. // Retrieve the reservation and sanity check the address reserved.
  3020. ConstHostPtr host = hosts_cfg->get6(234, DuidPtr(), hwaddr);
  3021. ASSERT_TRUE(host);
  3022. IPv6ResrvRange resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
  3023. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3024. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_NA,
  3025. IOAddress("2001:db8:2::abcd")),
  3026. resrv));
  3027. // This reservation should be solely assigned to the subnet 234,
  3028. // and not to other two.
  3029. EXPECT_FALSE(hosts_cfg->get6(123, DuidPtr(), hwaddr));
  3030. EXPECT_FALSE(hosts_cfg->get6(542, DuidPtr(), hwaddr));
  3031. // Do the same test for the DUID based reservation.
  3032. std::vector<uint8_t> duid_vec;
  3033. for (int i = 1; i < 0xb; ++i) {
  3034. duid_vec.push_back(static_cast<uint8_t>(i));
  3035. }
  3036. DuidPtr duid(new DUID(duid_vec));
  3037. host = hosts_cfg->get6(234, duid);
  3038. ASSERT_TRUE(host);
  3039. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
  3040. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3041. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_NA,
  3042. IOAddress("2001:db8:2::1234")),
  3043. resrv));
  3044. EXPECT_FALSE(hosts_cfg->get6(123, duid));
  3045. EXPECT_FALSE(hosts_cfg->get6(542, duid));
  3046. // The HW address used for one of the reservations in the subnet 542
  3047. // consists of numbers from 6 to 1. So, let's just reverse the order
  3048. // of the address from the previous test.
  3049. hwaddr->hwaddr_.assign(hwaddr_vec.rbegin(), hwaddr_vec.rend());
  3050. host = hosts_cfg->get6(542, DuidPtr(), hwaddr);
  3051. EXPECT_TRUE(host);
  3052. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_PD);
  3053. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3054. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_PD,
  3055. IOAddress("2001:db8:3:1::"),
  3056. 96), resrv));
  3057. // This reservation must not belong to other subnets.
  3058. EXPECT_FALSE(hosts_cfg->get6(123, DuidPtr(), hwaddr));
  3059. EXPECT_FALSE(hosts_cfg->get6(234, DuidPtr(), hwaddr));
  3060. // Repeat the test for the DUID based reservation in this subnet.
  3061. duid.reset(new DUID(std::vector<uint8_t>(duid_vec.rbegin(),
  3062. duid_vec.rend())));
  3063. host = hosts_cfg->get6(542, duid);
  3064. ASSERT_TRUE(host);
  3065. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_PD);
  3066. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3067. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_PD,
  3068. IOAddress("2001:db8:3:2::"),
  3069. 96), resrv));
  3070. EXPECT_FALSE(hosts_cfg->get6(123, duid));
  3071. EXPECT_FALSE(hosts_cfg->get6(234, duid));
  3072. }
  3073. // This test verifies that the bogus host reservation would trigger a
  3074. // server configuration error.
  3075. TEST_F(Dhcp6ParserTest, reservationBogus) {
  3076. // Case 1: misspelled "duid" parameter.
  3077. ConstElementPtr x;
  3078. string config = "{ " + genIfaceConfig() + ","
  3079. "\"rebind-timer\": 2000, "
  3080. "\"renew-timer\": 1000, "
  3081. "\"subnet6\": [ "
  3082. " { "
  3083. " \"pools\": [ ],"
  3084. " \"subnet\": \"2001:db8:3::/64\", "
  3085. " \"id\": 542,"
  3086. " \"reservations\": ["
  3087. " {"
  3088. " \"dui\": \"0A:09:08:07:06:05:04:03:02:01\","
  3089. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  3090. " \"hostname\": \"\""
  3091. " }"
  3092. " ]"
  3093. " } "
  3094. "], "
  3095. "\"preferred-lifetime\": 3000,"
  3096. "\"valid-lifetime\": 4000 }";
  3097. ElementPtr json = Element::fromJSON(config);
  3098. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3099. checkResult(x, 1);
  3100. // Case 2: DUID and HW Address both specified.
  3101. config = "{ " + genIfaceConfig() + ","
  3102. "\"rebind-timer\": 2000, "
  3103. "\"renew-timer\": 1000, "
  3104. "\"subnet6\": [ "
  3105. " { "
  3106. " \"pools\": [ ],"
  3107. " \"subnet\": \"2001:db8:3::/64\", "
  3108. " \"id\": 542,"
  3109. " \"reservations\": ["
  3110. " {"
  3111. " \"hw-address\": \"01:02:03:04:05:06\","
  3112. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\","
  3113. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  3114. " \"hostname\": \"\""
  3115. " }"
  3116. " ]"
  3117. " } "
  3118. "], "
  3119. "\"preferred-lifetime\": 3000,"
  3120. "\"valid-lifetime\": 4000 }";
  3121. json = Element::fromJSON(config);
  3122. // Remove existing configuration, if any.
  3123. CfgMgr::instance().clear();
  3124. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3125. checkResult(x, 1);
  3126. // Case 3: Neither ip address nor hostname specified.
  3127. config = "{ " + genIfaceConfig() + ","
  3128. "\"rebind-timer\": 2000, "
  3129. "\"renew-timer\": 1000, "
  3130. "\"subnet6\": [ "
  3131. " { "
  3132. " \"pools\": [ ],"
  3133. " \"subnet\": \"2001:db8:3::/64\", "
  3134. " \"id\": 542,"
  3135. " \"reservations\": ["
  3136. " {"
  3137. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\""
  3138. " }"
  3139. " ]"
  3140. " } "
  3141. "], "
  3142. "\"preferred-lifetime\": 3000,"
  3143. "\"valid-lifetime\": 4000 }";
  3144. json = Element::fromJSON(config);
  3145. // Remove existing configuration, if any.
  3146. CfgMgr::instance().clear();
  3147. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3148. checkResult(x, 1);
  3149. }
  3150. /// The goal of this test is to verify that configuration can include
  3151. /// MAC/Hardware sources. This test also checks if the aliases are
  3152. /// handled properly (rfc6939 = client-addr-relay, rfc4649 = remote-id,
  3153. /// rfc4580 = subscriber-id).
  3154. TEST_F(Dhcp6ParserTest, macSources) {
  3155. ConstElementPtr status;
  3156. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3157. Element::fromJSON("{ " + genIfaceConfig() + ","
  3158. "\"mac-sources\": [ \"rfc6939\", \"rfc4649\", \"rfc4580\","
  3159. "\"client-link-addr-option\", \"remote-id\", \"subscriber-id\"],"
  3160. "\"preferred-lifetime\": 3000,"
  3161. "\"rebind-timer\": 2000, "
  3162. "\"renew-timer\": 1000, "
  3163. "\"subnet6\": [ ], "
  3164. "\"valid-lifetime\": 4000 }")));
  3165. // returned value should be 0 (success)
  3166. checkResult(status, 0);
  3167. CfgMACSources mac_sources = CfgMgr::instance().getStagingCfg()->getMACSources().get();
  3168. ASSERT_EQ(6, mac_sources.size());
  3169. // Let's check the aliases. They should be recognized to their base methods.
  3170. EXPECT_EQ(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION, mac_sources[0]);
  3171. EXPECT_EQ(HWAddr::HWADDR_SOURCE_REMOTE_ID, mac_sources[1]);
  3172. EXPECT_EQ(HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID, mac_sources[2]);
  3173. // Let's check if the actual methods are recognized properly.
  3174. EXPECT_EQ(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION, mac_sources[3]);
  3175. EXPECT_EQ(HWAddr::HWADDR_SOURCE_REMOTE_ID, mac_sources[4]);
  3176. EXPECT_EQ(HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID, mac_sources[5]);
  3177. }
  3178. /// The goal of this test is to verify that MAC sources configuration can be
  3179. /// empty.
  3180. TEST_F(Dhcp6ParserTest, macSourcesEmpty) {
  3181. ConstElementPtr status;
  3182. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3183. Element::fromJSON("{ " + genIfaceConfig() + ","
  3184. "\"mac-sources\": [ ],"
  3185. "\"preferred-lifetime\": 3000,"
  3186. "\"rebind-timer\": 2000, "
  3187. "\"renew-timer\": 1000, "
  3188. "\"subnet6\": [ ], "
  3189. "\"valid-lifetime\": 4000 }")));
  3190. // returned value should be 0 (success)
  3191. checkResult(status, 0);
  3192. CfgMACSources mac_sources = CfgMgr::instance().getStagingCfg()->getMACSources().get();
  3193. EXPECT_EQ(0, mac_sources.size());
  3194. }
  3195. /// The goal of this test is to verify that MAC sources configuration can
  3196. /// only use valid parameters.
  3197. TEST_F(Dhcp6ParserTest, macSourcesBogus) {
  3198. ConstElementPtr status;
  3199. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3200. Element::fromJSON("{ " + genIfaceConfig() + ","
  3201. "\"mac-sources\": [ \"from-wire\" ],"
  3202. "\"preferred-lifetime\": 3000,"
  3203. "\"rebind-timer\": 2000, "
  3204. "\"renew-timer\": 1000, "
  3205. "\"subnet6\": [ ], "
  3206. "\"valid-lifetime\": 4000 }")));
  3207. // returned value should be 1 (failure)
  3208. checkResult(status, 1);
  3209. }
  3210. /// The goal of this test is to verify that Host Reservation modes can be
  3211. /// specified on a per-subnet basis.
  3212. TEST_F(Dhcp6ParserTest, hostReservationPerSubnet) {
  3213. /// - Configuration:
  3214. /// - only addresses (no prefixes)
  3215. /// - 4 subnets with:
  3216. /// - 2001:db8:1::/64 (all reservations enabled)
  3217. /// - 2001:db8:2::/64 (out-of-pool reservations)
  3218. /// - 2001:db8:3::/64 (reservations disabled)
  3219. /// - 2001:db8:3::/64 (reservations not specified)
  3220. const char* HR_CONFIG =
  3221. "{"
  3222. "\"preferred-lifetime\": 3000,"
  3223. "\"rebind-timer\": 2000, "
  3224. "\"renew-timer\": 1000, "
  3225. "\"subnet6\": [ { "
  3226. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  3227. " \"subnet\": \"2001:db8:1::/48\", "
  3228. " \"reservation-mode\": \"all\""
  3229. " },"
  3230. " {"
  3231. " \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  3232. " \"subnet\": \"2001:db8:2::/48\", "
  3233. " \"reservation-mode\": \"out-of-pool\""
  3234. " },"
  3235. " {"
  3236. " \"pools\": [ { \"pool\": \"2001:db8:3::/64\" } ],"
  3237. " \"subnet\": \"2001:db8:3::/48\", "
  3238. " \"reservation-mode\": \"disabled\""
  3239. " },"
  3240. " {"
  3241. " \"pools\": [ { \"pool\": \"2001:db8:4::/64\" } ],"
  3242. " \"subnet\": \"2001:db8:4::/48\" "
  3243. " } ],"
  3244. "\"valid-lifetime\": 4000 }";
  3245. ConstElementPtr status;
  3246. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3247. Element::fromJSON(HR_CONFIG)));
  3248. // returned value should be 0 (success)
  3249. checkResult(status, 0);
  3250. CfgMgr::instance().commit();
  3251. // Let's get all subnets and check that there are 4 of them.
  3252. ConstCfgSubnets6Ptr subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6();
  3253. ASSERT_TRUE(subnets);
  3254. const Subnet6Collection* subnet_col = subnets->getAll();
  3255. ASSERT_EQ(4, subnet_col->size()); // We expect 4 subnets
  3256. // Let's check if the parsed subnets have correct HR modes.
  3257. // Subnet 1
  3258. Subnet6Ptr subnet;
  3259. subnet = subnets->selectSubnet(IOAddress("2001:db8:1::1"));
  3260. ASSERT_TRUE(subnet);
  3261. EXPECT_EQ(Subnet::HR_ALL, subnet->getHostReservationMode());
  3262. // Subnet 2
  3263. subnet = subnets->selectSubnet(IOAddress("2001:db8:2::1"));
  3264. ASSERT_TRUE(subnet);
  3265. EXPECT_EQ(Subnet::HR_OUT_OF_POOL, subnet->getHostReservationMode());
  3266. // Subnet 3
  3267. subnet = subnets->selectSubnet(IOAddress("2001:db8:3::1"));
  3268. ASSERT_TRUE(subnet);
  3269. EXPECT_EQ(Subnet::HR_DISABLED, subnet->getHostReservationMode());
  3270. // Subnet 4
  3271. subnet = subnets->selectSubnet(IOAddress("2001:db8:4::1"));
  3272. ASSERT_TRUE(subnet);
  3273. EXPECT_EQ(Subnet::HR_ALL, subnet->getHostReservationMode());
  3274. }
  3275. /// The goal of this test is to verify that configuration can include
  3276. /// Relay Supplied options (specified as numbers).
  3277. TEST_F(Dhcp6ParserTest, rsooNumbers) {
  3278. ConstElementPtr status;
  3279. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3280. Element::fromJSON("{ " + genIfaceConfig() + ","
  3281. "\"relay-supplied-options\": [ \"10\", \"20\", \"30\" ],"
  3282. "\"preferred-lifetime\": 3000,"
  3283. "\"rebind-timer\": 2000, "
  3284. "\"renew-timer\": 1000, "
  3285. "\"subnet6\": [ ], "
  3286. "\"valid-lifetime\": 4000 }")));
  3287. // returned value should be 0 (success)
  3288. checkResult(status, 0);
  3289. // The following codes should be enabled now
  3290. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(10));
  3291. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(20));
  3292. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(30));
  3293. // This option is on the IANA list, so it should be allowed all the time
  3294. // (http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml)
  3295. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3296. ->enabled(D6O_ERP_LOCAL_DOMAIN_NAME));
  3297. // Those options are not enabled
  3298. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(25));
  3299. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(1));
  3300. }
  3301. /// The goal of this test is to verify that configuration can include
  3302. /// Relay Supplied options (specified as names).
  3303. TEST_F(Dhcp6ParserTest, rsooNames) {
  3304. ConstElementPtr status;
  3305. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3306. Element::fromJSON("{ " + genIfaceConfig() + ","
  3307. "\"relay-supplied-options\": [ \"dns-servers\", \"remote-id\" ],"
  3308. "\"preferred-lifetime\": 3000,"
  3309. "\"rebind-timer\": 2000, "
  3310. "\"renew-timer\": 1000, "
  3311. "\"subnet6\": [ ], "
  3312. "\"valid-lifetime\": 4000 }")));
  3313. // returned value should be 0 (success)
  3314. checkResult(status, 0);
  3315. for (uint16_t code = 0; code < D6O_NAME_SERVERS; ++code) {
  3316. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3317. ->enabled(code)) << " for option code " << code;
  3318. }
  3319. // The following code should be enabled now
  3320. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3321. ->enabled(D6O_NAME_SERVERS));
  3322. for (uint16_t code = D6O_NAME_SERVERS + 1; code < D6O_REMOTE_ID; ++code) {
  3323. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3324. ->enabled(code)) << " for option code " << code;
  3325. }
  3326. // Check remote-id. It should be enabled.
  3327. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3328. ->enabled(D6O_REMOTE_ID));
  3329. for (uint16_t code = D6O_REMOTE_ID + 1; code < D6O_ERP_LOCAL_DOMAIN_NAME; ++code) {
  3330. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3331. ->enabled(code)) << " for option code " << code;
  3332. }
  3333. // This option is on the IANA list, so it should be allowed all the time
  3334. // (http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml)
  3335. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3336. ->enabled(D6O_ERP_LOCAL_DOMAIN_NAME));
  3337. for (uint16_t code = D6O_ERP_LOCAL_DOMAIN_NAME + 1; code < 300; ++code) {
  3338. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3339. ->enabled(code)) << " for option code " << code;
  3340. }
  3341. }
  3342. TEST_F(Dhcp6ParserTest, rsooNegativeNumber) {
  3343. ConstElementPtr status;
  3344. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3345. Element::fromJSON("{ " + genIfaceConfig() + ","
  3346. "\"relay-supplied-options\": [ \"80\", \"-2\" ],"
  3347. "\"preferred-lifetime\": 3000,"
  3348. "\"rebind-timer\": 2000, "
  3349. "\"renew-timer\": 1000, "
  3350. "\"subnet6\": [ ], "
  3351. "\"valid-lifetime\": 4000 }")));
  3352. // returned value should be 0 (success)
  3353. checkResult(status, 1);
  3354. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3355. }
  3356. TEST_F(Dhcp6ParserTest, rsooBogusName) {
  3357. ConstElementPtr status;
  3358. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3359. Element::fromJSON("{ " + genIfaceConfig() + ","
  3360. "\"relay-supplied-options\": [ \"bogus\", \"dns-servers\" ],"
  3361. "\"preferred-lifetime\": 3000,"
  3362. "\"rebind-timer\": 2000, "
  3363. "\"renew-timer\": 1000, "
  3364. "\"subnet6\": [ ], "
  3365. "\"valid-lifetime\": 4000 }")));
  3366. // returned value should be 0 (success)
  3367. checkResult(status, 1);
  3368. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3369. }
  3370. /// Check that the decline-probation-period value can be set properly.
  3371. TEST_F(Dhcp6ParserTest, declineTimerDefault) {
  3372. ConstElementPtr status;
  3373. string config_txt = "{ " + genIfaceConfig() + ","
  3374. "\"subnet6\": [ ] "
  3375. "}";
  3376. ElementPtr config = Element::fromJSON(config_txt);
  3377. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config));
  3378. // returned value should be 0 (success)
  3379. checkResult(status, 0);
  3380. // The value of decline-probation-period must be equal to the
  3381. // default value.
  3382. EXPECT_EQ(DEFAULT_DECLINE_PROBATION_PERIOD,
  3383. CfgMgr::instance().getStagingCfg()->getDeclinePeriod());
  3384. }
  3385. /// Check that the decline-probation-period value can be set properly.
  3386. TEST_F(Dhcp6ParserTest, declineTimer) {
  3387. ConstElementPtr status;
  3388. string config = "{ " + genIfaceConfig() + "," +
  3389. "\"decline-probation-period\": 12345,"
  3390. "\"subnet6\": [ ]"
  3391. "}";
  3392. ElementPtr json = Element::fromJSON(config);
  3393. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3394. // returned value should be 0 (success)
  3395. checkResult(status, 0);
  3396. // The value of decline-probation-period must be equal to the
  3397. // value specified.
  3398. EXPECT_EQ(12345,
  3399. CfgMgr::instance().getStagingCfg()->getDeclinePeriod());
  3400. }
  3401. /// Check that an incorrect decline-probation-period value will be caught.
  3402. TEST_F(Dhcp6ParserTest, declineTimerError) {
  3403. ConstElementPtr status;
  3404. string config = "{ " + genIfaceConfig() + "," +
  3405. "\"decline-probation-period\": \"soon\","
  3406. "\"subnet6\": [ ]"
  3407. "}";
  3408. ElementPtr json = Element::fromJSON(config);
  3409. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3410. // returned value should be 1 (error)
  3411. checkResult(status, 1);
  3412. // Check that the error contains error position.
  3413. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3414. }
  3415. // Check that configuration for the expired leases processing may be
  3416. // specified.
  3417. TEST_F(Dhcp6ParserTest, expiredLeasesProcessing) {
  3418. // Create basic configuration with the expiration specific parameters.
  3419. string config = "{ " + genIfaceConfig() + "," +
  3420. "\"expired-leases-processing\": "
  3421. "{"
  3422. " \"reclaim-timer-wait-time\": 20,"
  3423. " \"flush-reclaimed-timer-wait-time\": 35,"
  3424. " \"hold-reclaimed-time\": 1800,"
  3425. " \"max-reclaim-leases\": 50,"
  3426. " \"max-reclaim-time\": 100,"
  3427. " \"unwarned-reclaim-cycles\": 10"
  3428. "},"
  3429. "\"subnet6\": [ ]"
  3430. "}";
  3431. ElementPtr json = Element::fromJSON(config);
  3432. ConstElementPtr status;
  3433. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3434. // Returned value should be 0 (success)
  3435. checkResult(status, 0);
  3436. // The value of decline-probation-period must be equal to the
  3437. // value specified.
  3438. CfgExpirationPtr cfg = CfgMgr::instance().getStagingCfg()->getCfgExpiration();
  3439. ASSERT_TRUE(cfg);
  3440. // Verify that parameters are correct.
  3441. EXPECT_EQ(20, cfg->getReclaimTimerWaitTime());
  3442. EXPECT_EQ(35, cfg->getFlushReclaimedTimerWaitTime());
  3443. EXPECT_EQ(1800, cfg->getHoldReclaimedTime());
  3444. EXPECT_EQ(50, cfg->getMaxReclaimLeases());
  3445. EXPECT_EQ(100, cfg->getMaxReclaimTime());
  3446. EXPECT_EQ(10, cfg->getUnwarnedReclaimCycles());
  3447. }
  3448. // Check that invalid configuration for the expired leases processing is
  3449. // causing an error.
  3450. TEST_F(Dhcp6ParserTest, expiredLeasesProcessingError) {
  3451. // Create basic configuration with the expiration specific parameters.
  3452. // One of the parameters holds invalid value.
  3453. string config = "{ " + genIfaceConfig() + "," +
  3454. "\"expired-leases-processing\": "
  3455. "{"
  3456. " \"reclaim-timer-wait-time\": -5,"
  3457. " \"flush-reclaimed-timer-wait-time\": 35,"
  3458. " \"hold-reclaimed-time\": 1800,"
  3459. " \"max-reclaim-leases\": 50,"
  3460. " \"max-reclaim-time\": 100,"
  3461. " \"unwarned-reclaim-cycles\": 10"
  3462. "},"
  3463. "\"subnet6\": [ ]"
  3464. "}";
  3465. ElementPtr json = Element::fromJSON(config);
  3466. ConstElementPtr status;
  3467. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3468. // Returned value should be 0 (error)
  3469. checkResult(status, 1);
  3470. // Check that the error contains error position.
  3471. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3472. }
  3473. // Verifies that simple list of valid classes parses and
  3474. // is staged for commit.
  3475. TEST_F(Dhcp6ParserTest, validClientClassDictionary) {
  3476. string config = "{ " + genIfaceConfig() + ","
  3477. "\"preferred-lifetime\": 3000, \n"
  3478. "\"rebind-timer\": 2000, \n"
  3479. "\"renew-timer\": 1000, \n"
  3480. "\"client-classes\" : [ \n"
  3481. " { \n"
  3482. " \"name\": \"one\" \n"
  3483. " }, \n"
  3484. " { \n"
  3485. " \"name\": \"two\" \n"
  3486. " }, \n"
  3487. " { \n"
  3488. " \"name\": \"three\" \n"
  3489. " } \n"
  3490. "], \n"
  3491. "\"subnet6\": [ { \n"
  3492. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ], \n"
  3493. " \"subnet\": \"2001:db8:1::/64\" } ], \n"
  3494. "\"valid-lifetime\": 4000 } \n";
  3495. ConstElementPtr status;
  3496. ElementPtr json = Element::fromJSON(config);
  3497. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3498. ASSERT_TRUE(status);
  3499. checkResult(status, 0);
  3500. // We check staging config because CfgMgr::commit hasn't been executed.
  3501. ClientClassDictionaryPtr dictionary;
  3502. dictionary = CfgMgr::instance().getStagingCfg()->getClientClassDictionary();
  3503. ASSERT_TRUE(dictionary);
  3504. EXPECT_EQ(3, dictionary->getClasses()->size());
  3505. // Execute the commit
  3506. ASSERT_NO_THROW(CfgMgr::instance().commit());
  3507. // Verify that after commit, the current config has the correct dictionary
  3508. dictionary = CfgMgr::instance().getCurrentCfg()->getClientClassDictionary();
  3509. ASSERT_TRUE(dictionary);
  3510. EXPECT_EQ(3, dictionary->getClasses()->size());
  3511. }
  3512. // Verifies that a class list containing an invalid
  3513. // class definition causes a configuration error.
  3514. TEST_F(Dhcp6ParserTest, invalidClientClassDictionary) {
  3515. string config = "{ " + genIfaceConfig() + "," +
  3516. "\"valid-lifetime\": 4000, \n"
  3517. "\"rebind-timer\": 2000, \n"
  3518. "\"renew-timer\": 1000, \n"
  3519. "\"client-classes\" : [ \n"
  3520. " { \n"
  3521. " \"name\": \"one\", \n"
  3522. " \"bogus\": \"bad\" \n"
  3523. " } \n"
  3524. "], \n"
  3525. "\"subnet4\": [ { \n"
  3526. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], \n"
  3527. " \"subnet\": \"192.0.2.0/24\" \n"
  3528. " } ] \n"
  3529. "} \n";
  3530. ConstElementPtr status;
  3531. ElementPtr json = Element::fromJSON(config);
  3532. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3533. ASSERT_TRUE(status);
  3534. checkResult(status, 1);
  3535. }
  3536. };