config_parser_unittest.cc 149 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068
  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. // The option definition should now be available in the CfgMgr.
  1365. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1366. ASSERT_TRUE(def);
  1367. // Verify that the option definition data is valid.
  1368. EXPECT_EQ("foo", def->getName());
  1369. EXPECT_EQ(100, def->getCode());
  1370. EXPECT_FALSE(def->getArrayType());
  1371. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, def->getType());
  1372. }
  1373. // The goal of this test is to check whether an option definition
  1374. // that defines an option carrying a record of data fields can
  1375. // be created.
  1376. TEST_F(Dhcp6ParserTest, optionDefRecord) {
  1377. // Configuration string.
  1378. std::string config =
  1379. "{ \"option-def\": [ {"
  1380. " \"name\": \"foo\","
  1381. " \"code\": 100,"
  1382. " \"type\": \"record\","
  1383. " \"record-types\": \"uint16, ipv4-address, ipv6-address, string\","
  1384. " \"space\": \"isc\""
  1385. " } ]"
  1386. "}";
  1387. ElementPtr json = Element::fromJSON(config);
  1388. // Make sure that the particular option definition does not exist.
  1389. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1390. getCfgOptionDef()->get("isc", 100);
  1391. ASSERT_FALSE(def);
  1392. // Use the configuration string to create new option definition.
  1393. ConstElementPtr status;
  1394. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1395. ASSERT_TRUE(status);
  1396. checkResult(status, 0);
  1397. // The option definition should now be available in the CfgMgr.
  1398. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1399. ASSERT_TRUE(def);
  1400. // Check the option data.
  1401. EXPECT_EQ("foo", def->getName());
  1402. EXPECT_EQ(100, def->getCode());
  1403. EXPECT_EQ(OPT_RECORD_TYPE, def->getType());
  1404. EXPECT_FALSE(def->getArrayType());
  1405. // The option comprises the record of data fields. Verify that all
  1406. // fields are present and they are of the expected types.
  1407. const OptionDefinition::RecordFieldsCollection& record_fields =
  1408. def->getRecordFields();
  1409. ASSERT_EQ(4, record_fields.size());
  1410. EXPECT_EQ(OPT_UINT16_TYPE, record_fields[0]);
  1411. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, record_fields[1]);
  1412. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, record_fields[2]);
  1413. EXPECT_EQ(OPT_STRING_TYPE, record_fields[3]);
  1414. }
  1415. // The goal of this test is to verify that multiple option definitions
  1416. // can be created.
  1417. TEST_F(Dhcp6ParserTest, optionDefMultiple) {
  1418. // Configuration string.
  1419. std::string config =
  1420. "{ \"option-def\": [ {"
  1421. " \"name\": \"foo\","
  1422. " \"code\": 100,"
  1423. " \"type\": \"uint32\","
  1424. " \"space\": \"isc\""
  1425. " },"
  1426. " {"
  1427. " \"name\": \"foo-2\","
  1428. " \"code\": 101,"
  1429. " \"type\": \"ipv4-address\","
  1430. " \"space\": \"isc\""
  1431. " } ]"
  1432. "}";
  1433. ElementPtr json = Element::fromJSON(config);
  1434. // Make sure that the option definitions do not exist yet.
  1435. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1436. getCfgOptionDef()->get("isc", 100));
  1437. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1438. getCfgOptionDef()->get("isc", 101));
  1439. // Use the configuration string to create new option definitions.
  1440. ConstElementPtr status;
  1441. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1442. ASSERT_TRUE(status);
  1443. checkResult(status, 0);
  1444. // Check the first definition we have created.
  1445. OptionDefinitionPtr def1 = CfgMgr::instance().getStagingCfg()->
  1446. getCfgOptionDef()->get("isc", 100);
  1447. ASSERT_TRUE(def1);
  1448. // Check the option data.
  1449. EXPECT_EQ("foo", def1->getName());
  1450. EXPECT_EQ(100, def1->getCode());
  1451. EXPECT_EQ(OPT_UINT32_TYPE, def1->getType());
  1452. EXPECT_FALSE(def1->getArrayType());
  1453. // Check the second option definition we have created.
  1454. OptionDefinitionPtr def2 = CfgMgr::instance().getStagingCfg()->
  1455. getCfgOptionDef()->get("isc", 101);
  1456. ASSERT_TRUE(def2);
  1457. // Check the option data.
  1458. EXPECT_EQ("foo-2", def2->getName());
  1459. EXPECT_EQ(101, def2->getCode());
  1460. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def2->getType());
  1461. EXPECT_FALSE(def2->getArrayType());
  1462. }
  1463. // The goal of this test is to verify that the duplicated option
  1464. // definition is not accepted.
  1465. TEST_F(Dhcp6ParserTest, optionDefDuplicate) {
  1466. // Configuration string. Both option definitions have
  1467. // the same code and belong to the same option space.
  1468. // This configuration should not be accepted.
  1469. std::string config =
  1470. "{ \"option-def\": [ {"
  1471. " \"name\": \"foo\","
  1472. " \"code\": 100,"
  1473. " \"type\": \"uint32\","
  1474. " \"space\": \"isc\""
  1475. " },"
  1476. " {"
  1477. " \"name\": \"foo-2\","
  1478. " \"code\": 100,"
  1479. " \"type\": \"ipv4-address\","
  1480. " \"space\": \"isc\""
  1481. " } ]"
  1482. "}";
  1483. ElementPtr json = Element::fromJSON(config);
  1484. // Make sure that the option definition does not exist yet.
  1485. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1486. getCfgOptionDef()->get("isc", 100));
  1487. // Use the configuration string to create new option definitions.
  1488. ConstElementPtr status;
  1489. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1490. ASSERT_TRUE(status);
  1491. checkResult(status, 1);
  1492. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1493. }
  1494. // The goal of this test is to verify that the option definition
  1495. // comprising an array of uint32 values can be created.
  1496. TEST_F(Dhcp6ParserTest, optionDefArray) {
  1497. // Configuration string. Created option definition should
  1498. // comprise an array of uint32 values.
  1499. std::string config =
  1500. "{ \"option-def\": [ {"
  1501. " \"name\": \"foo\","
  1502. " \"code\": 100,"
  1503. " \"type\": \"uint32\","
  1504. " \"array\": True,"
  1505. " \"space\": \"isc\""
  1506. " } ]"
  1507. "}";
  1508. ElementPtr json = Element::fromJSON(config);
  1509. // Make sure that the particular option definition does not exist.
  1510. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1511. getCfgOptionDef()->get("isc", 100);
  1512. ASSERT_FALSE(def);
  1513. // Use the configuration string to create new option definition.
  1514. ConstElementPtr status;
  1515. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1516. ASSERT_TRUE(status);
  1517. checkResult(status, 0);
  1518. // The option definition should now be available in the CfgMgr.
  1519. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1520. ASSERT_TRUE(def);
  1521. // Check the option data.
  1522. EXPECT_EQ("foo", def->getName());
  1523. EXPECT_EQ(100, def->getCode());
  1524. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  1525. EXPECT_TRUE(def->getArrayType());
  1526. }
  1527. // The purpose of this test to verify that encapsulated option
  1528. // space name may be specified.
  1529. TEST_F(Dhcp6ParserTest, optionDefEncapsulate) {
  1530. // Configuration string. Included the encapsulated
  1531. // option space name.
  1532. std::string config =
  1533. "{ \"option-def\": [ {"
  1534. " \"name\": \"foo\","
  1535. " \"code\": 100,"
  1536. " \"type\": \"uint32\","
  1537. " \"space\": \"isc\","
  1538. " \"encapsulate\": \"sub-opts-space\""
  1539. " } ]"
  1540. "}";
  1541. ElementPtr json = Element::fromJSON(config);
  1542. // Make sure that the particular option definition does not exist.
  1543. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1544. getCfgOptionDef()->get("isc", 100);
  1545. ASSERT_FALSE(def);
  1546. // Use the configuration string to create new option definition.
  1547. ConstElementPtr status;
  1548. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1549. ASSERT_TRUE(status);
  1550. checkResult(status, 0);
  1551. // The option definition should now be available in the CfgMgr.
  1552. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1553. ASSERT_TRUE(def);
  1554. // Check the option data.
  1555. EXPECT_EQ("foo", def->getName());
  1556. EXPECT_EQ(100, def->getCode());
  1557. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  1558. EXPECT_FALSE(def->getArrayType());
  1559. EXPECT_EQ("sub-opts-space", def->getEncapsulatedSpace());
  1560. }
  1561. /// The purpose of this test is to verify that the option definition
  1562. /// with invalid name is not accepted.
  1563. TEST_F(Dhcp6ParserTest, optionDefInvalidName) {
  1564. // Configuration string. The option name is invalid as it
  1565. // contains the % character.
  1566. std::string config =
  1567. "{ \"option-def\": [ {"
  1568. " \"name\": \"invalid%name\","
  1569. " \"code\": 100,"
  1570. " \"type\": \"string\","
  1571. " \"space\": \"isc\""
  1572. " } ]"
  1573. "}";
  1574. ElementPtr json = Element::fromJSON(config);
  1575. // Use the configuration string to create new option definition.
  1576. ConstElementPtr status;
  1577. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1578. ASSERT_TRUE(status);
  1579. // Expecting parsing error (error code 1).
  1580. checkResult(status, 1);
  1581. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1582. }
  1583. /// The purpose of this test is to verify that the option definition
  1584. /// with invalid type is not accepted.
  1585. TEST_F(Dhcp6ParserTest, optionDefInvalidType) {
  1586. // Configuration string. The option type is invalid. It is
  1587. // "sting" instead of "string".
  1588. std::string config =
  1589. "{ \"option-def\": [ {"
  1590. " \"name\": \"foo\","
  1591. " \"code\": 100,"
  1592. " \"type\": \"sting\","
  1593. " \"space\": \"isc\""
  1594. " } ]"
  1595. "}";
  1596. ElementPtr json = Element::fromJSON(config);
  1597. // Use the configuration string to create new option definition.
  1598. ConstElementPtr status;
  1599. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1600. ASSERT_TRUE(status);
  1601. // Expecting parsing error (error code 1).
  1602. checkResult(status, 1);
  1603. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1604. }
  1605. /// The purpose of this test is to verify that the option definition
  1606. /// with invalid type is not accepted.
  1607. TEST_F(Dhcp6ParserTest, optionDefInvalidRecordType) {
  1608. // Configuration string. The third of the record fields
  1609. // is invalid. It is "sting" instead of "string".
  1610. std::string config =
  1611. "{ \"option-def\": [ {"
  1612. " \"name\": \"foo\","
  1613. " \"code\": 100,"
  1614. " \"type\": \"record\","
  1615. " \"record-types\": \"uint32,uint8,sting\","
  1616. " \"space\": \"isc\""
  1617. " } ]"
  1618. "}";
  1619. ElementPtr json = Element::fromJSON(config);
  1620. // Use the configuration string to create new option definition.
  1621. ConstElementPtr status;
  1622. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1623. ASSERT_TRUE(status);
  1624. // Expecting parsing error (error code 1).
  1625. checkResult(status, 1);
  1626. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1627. }
  1628. /// The goal of this test is to verify that the invalid encapsulated
  1629. /// option space name is not accepted.
  1630. TEST_F(Dhcp6ParserTest, optionDefInvalidEncapsulatedSpace) {
  1631. // Configuration string. The encapsulated option space
  1632. // name is invalid (% character is not allowed).
  1633. std::string config =
  1634. "{ \"option-def\": [ {"
  1635. " \"name\": \"foo\","
  1636. " \"code\": 100,"
  1637. " \"type\": \"uint32\","
  1638. " \"space\": \"isc\","
  1639. " \"encapsulate\": \"invalid%space%name\""
  1640. " } ]"
  1641. "}";
  1642. ElementPtr json = Element::fromJSON(config);
  1643. // Use the configuration string to create new option definition.
  1644. ConstElementPtr status;
  1645. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1646. ASSERT_TRUE(status);
  1647. // Expecting parsing error (error code 1).
  1648. checkResult(status, 1);
  1649. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1650. }
  1651. /// The goal of this test is to verify that the encapsulated
  1652. /// option space name can't be specified for the option that
  1653. /// comprises an array of data fields.
  1654. TEST_F(Dhcp6ParserTest, optionDefEncapsulatedSpaceAndArray) {
  1655. // Configuration string. The encapsulated option space
  1656. // name is set to non-empty value and the array flag
  1657. // is set.
  1658. std::string config =
  1659. "{ \"option-def\": [ {"
  1660. " \"name\": \"foo\","
  1661. " \"code\": 100,"
  1662. " \"type\": \"uint32\","
  1663. " \"array\": True,"
  1664. " \"space\": \"isc\","
  1665. " \"encapsulate\": \"valid-space-name\""
  1666. " } ]"
  1667. "}";
  1668. ElementPtr json = Element::fromJSON(config);
  1669. // Use the configuration string to create new option definition.
  1670. ConstElementPtr status;
  1671. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1672. ASSERT_TRUE(status);
  1673. // Expecting parsing error (error code 1).
  1674. checkResult(status, 1);
  1675. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1676. }
  1677. /// The goal of this test is to verify that the option may not
  1678. /// encapsulate option space it belongs to.
  1679. TEST_F(Dhcp6ParserTest, optionDefEncapsulateOwnSpace) {
  1680. // Configuration string. Option is set to encapsulate
  1681. // option space it belongs to.
  1682. std::string config =
  1683. "{ \"option-def\": [ {"
  1684. " \"name\": \"foo\","
  1685. " \"code\": 100,"
  1686. " \"type\": \"uint32\","
  1687. " \"space\": \"isc\","
  1688. " \"encapsulate\": \"isc\""
  1689. " } ]"
  1690. "}";
  1691. ElementPtr json = Element::fromJSON(config);
  1692. // Use the configuration string to create new option definition.
  1693. ConstElementPtr status;
  1694. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1695. ASSERT_TRUE(status);
  1696. // Expecting parsing error (error code 1).
  1697. checkResult(status, 1);
  1698. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1699. }
  1700. /// The purpose of this test is to verify that it is not allowed
  1701. /// to override the standard option (that belongs to dhcp6 option
  1702. /// space and has its definition) and that it is allowed to define
  1703. /// option in the dhcp6 option space that has a code which is not
  1704. /// used by any of the standard options.
  1705. TEST_F(Dhcp6ParserTest, optionStandardDefOverride) {
  1706. // Configuration string. The option code 100 is unassigned
  1707. // so it can be used for a custom option definition in
  1708. // dhcp6 option space.
  1709. std::string config =
  1710. "{ \"option-def\": [ {"
  1711. " \"name\": \"foo\","
  1712. " \"code\": 100,"
  1713. " \"type\": \"string\","
  1714. " \"space\": \"dhcp6\""
  1715. " } ]"
  1716. "}";
  1717. ElementPtr json = Element::fromJSON(config);
  1718. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1719. getCfgOptionDef()->get("dhcp6", 100);
  1720. ASSERT_FALSE(def);
  1721. // Use the configuration string to create new option definition.
  1722. ConstElementPtr status;
  1723. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1724. ASSERT_TRUE(status);
  1725. checkResult(status, 0);
  1726. // The option definition should now be available in the CfgMgr.
  1727. def = CfgMgr::instance().getStagingCfg()->
  1728. getCfgOptionDef()->get("dhcp6", 100);
  1729. ASSERT_TRUE(def);
  1730. // Check the option data.
  1731. EXPECT_EQ("foo", def->getName());
  1732. EXPECT_EQ(100, def->getCode());
  1733. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  1734. EXPECT_FALSE(def->getArrayType());
  1735. // The combination of option space and code is invalid. The 'dhcp6'
  1736. // option space groups standard options and the code 3 is reserved
  1737. // for one of them.
  1738. config =
  1739. "{ \"option-def\": [ {"
  1740. " \"name\": \"foo\","
  1741. " \"code\": 3,"
  1742. " \"type\": \"string\","
  1743. " \"space\": \"dhcp6\""
  1744. " } ]"
  1745. "}";
  1746. json = Element::fromJSON(config);
  1747. // Use the configuration string to create new option definition.
  1748. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1749. ASSERT_TRUE(status);
  1750. // Expecting parsing error (error code 1).
  1751. checkResult(status, 1);
  1752. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1753. /// @todo The option 63 is a standard DHCPv6 option. However, at this point
  1754. /// there is no definition for this option in libdhcp++, so it should be
  1755. /// allowed to define it from the configuration interface. This test will
  1756. /// have to be removed once definitions for remaining standard options are
  1757. /// created.
  1758. config =
  1759. "{ \"option-def\": [ {"
  1760. " \"name\": \"geolocation\","
  1761. " \"code\": 63,"
  1762. " \"type\": \"string\","
  1763. " \"space\": \"dhcp6\""
  1764. " } ]"
  1765. "}";
  1766. json = Element::fromJSON(config);
  1767. // Use the configuration string to create new option definition.
  1768. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1769. ASSERT_TRUE(status);
  1770. // Expecting success.
  1771. checkResult(status, 0);
  1772. def = CfgMgr::instance().getStagingCfg()->
  1773. getCfgOptionDef()->get("dhcp6", 63);
  1774. ASSERT_TRUE(def);
  1775. // Check the option data.
  1776. EXPECT_EQ("geolocation", def->getName());
  1777. EXPECT_EQ(63, def->getCode());
  1778. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  1779. EXPECT_FALSE(def->getArrayType());
  1780. }
  1781. // Goal of this test is to verify that global option
  1782. // data is configured for the subnet if the subnet
  1783. // configuration does not include options configuration.
  1784. TEST_F(Dhcp6ParserTest, optionDataDefaults) {
  1785. ConstElementPtr x;
  1786. string config = "{ " + genIfaceConfig() + ","
  1787. "\"preferred-lifetime\": 3000,"
  1788. "\"rebind-timer\": 2000,"
  1789. "\"renew-timer\": 1000,"
  1790. "\"option-data\": [ {"
  1791. " \"name\": \"subscriber-id\","
  1792. " \"data\": \"ABCDEF0105\","
  1793. " \"csv-format\": False"
  1794. " },"
  1795. " {"
  1796. " \"name\": \"preference\","
  1797. " \"data\": \"01\""
  1798. " } ],"
  1799. "\"subnet6\": [ { "
  1800. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  1801. " \"subnet\": \"2001:db8:1::/64\""
  1802. " } ],"
  1803. "\"valid-lifetime\": 4000 }";
  1804. ElementPtr json = Element::fromJSON(config);
  1805. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1806. checkResult(x, 0);
  1807. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1808. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1809. ASSERT_TRUE(subnet);
  1810. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  1811. ASSERT_EQ(2, options->size());
  1812. // Get the search index. Index #1 is to search using option code.
  1813. const OptionContainerTypeIndex& idx = options->get<1>();
  1814. // Get the options for specified index. Expecting one option to be
  1815. // returned but in theory we may have multiple options with the same
  1816. // code so we get the range.
  1817. std::pair<OptionContainerTypeIndex::const_iterator,
  1818. OptionContainerTypeIndex::const_iterator> range =
  1819. idx.equal_range(D6O_SUBSCRIBER_ID);
  1820. // Expect single option with the code equal to 38.
  1821. ASSERT_EQ(1, std::distance(range.first, range.second));
  1822. const uint8_t subid_expected[] = {
  1823. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  1824. };
  1825. // Check if option is valid in terms of code and carried data.
  1826. testOption(*range.first, D6O_SUBSCRIBER_ID, subid_expected,
  1827. sizeof(subid_expected));
  1828. range = idx.equal_range(D6O_PREFERENCE);
  1829. ASSERT_EQ(1, std::distance(range.first, range.second));
  1830. // Do another round of testing with second option.
  1831. const uint8_t pref_expected[] = {
  1832. 0x01
  1833. };
  1834. testOption(*range.first, D6O_PREFERENCE, pref_expected,
  1835. sizeof(pref_expected));
  1836. // Check that options with other option codes are not returned.
  1837. for (uint16_t code = 47; code < 57; ++code) {
  1838. range = idx.equal_range(code);
  1839. EXPECT_EQ(0, std::distance(range.first, range.second));
  1840. }
  1841. }
  1842. /// The goal of this test is to verify that two options having the same
  1843. /// option code can be added to different option spaces.
  1844. TEST_F(Dhcp6ParserTest, optionDataTwoSpaces) {
  1845. // This configuration string is to configure two options
  1846. // sharing the code 56 and having different definitions
  1847. // and belonging to the different option spaces.
  1848. // The option definition must be provided for the
  1849. // option that belongs to the 'isc' option space.
  1850. // The definition is not required for the option that
  1851. // belongs to the 'dhcp6' option space as it is the
  1852. // standard option.
  1853. string config = "{ " + genIfaceConfig() + ","
  1854. "\"preferred-lifetime\": 3000,"
  1855. "\"valid-lifetime\": 4000,"
  1856. "\"rebind-timer\": 2000,"
  1857. "\"renew-timer\": 1000,"
  1858. "\"option-data\": [ {"
  1859. " \"name\": \"subscriber-id\","
  1860. " \"data\": \"ABCDEF0105\","
  1861. " \"csv-format\": False"
  1862. " },"
  1863. " {"
  1864. " \"name\": \"foo\","
  1865. " \"space\": \"isc\","
  1866. " \"data\": \"1234\""
  1867. " } ],"
  1868. "\"option-def\": [ {"
  1869. " \"name\": \"foo\","
  1870. " \"code\": 38,"
  1871. " \"type\": \"uint32\","
  1872. " \"space\": \"isc\""
  1873. " } ],"
  1874. "\"subnet6\": [ { "
  1875. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  1876. " \"subnet\": \"2001:db8:1::/64\""
  1877. " } ]"
  1878. "}";
  1879. ConstElementPtr status;
  1880. ElementPtr json = Element::fromJSON(config);
  1881. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1882. ASSERT_TRUE(status);
  1883. checkResult(status, 0);
  1884. // Options should be now available for the subnet.
  1885. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1886. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1887. ASSERT_TRUE(subnet);
  1888. // Try to get the option from the space dhcp6.
  1889. OptionDescriptor desc1 = subnet->getCfgOption()->get("dhcp6", 38);
  1890. ASSERT_TRUE(desc1.option_);
  1891. EXPECT_EQ(38, desc1.option_->getType());
  1892. // Try to get the option from the space isc.
  1893. OptionDescriptor desc2 = subnet->getCfgOption()->get("isc", 38);
  1894. ASSERT_TRUE(desc2.option_);
  1895. EXPECT_EQ(38, desc1.option_->getType());
  1896. // Try to get the non-existing option from the non-existing
  1897. // option space and expect that option is not returned.
  1898. OptionDescriptor desc3 = subnet->getCfgOption()->get("non-existing", 38);
  1899. ASSERT_FALSE(desc3.option_);
  1900. }
  1901. // The goal of this test is to verify that it is possible to
  1902. // encapsulate option space containing some options with
  1903. // another option. In this test we create base option that
  1904. // encapsulates option space 'isc' that comprises two other
  1905. // options. Also, for all options their definitions are
  1906. // created.
  1907. TEST_F(Dhcp6ParserTest, optionDataEncapsulate) {
  1908. // @todo DHCP configurations has many dependencies between
  1909. // parameters. First of all, configuration for subnet is
  1910. // inherited from the global values. Thus subnet has to be
  1911. // configured when all global values have been configured.
  1912. // Also, an option can encapsulate another option only
  1913. // if the latter has been configured. For this reason in this
  1914. // test we created two-stage configuration where first we
  1915. // created options that belong to encapsulated option space.
  1916. // In the second stage we add the base option. Also, the Subnet
  1917. // object is configured in the second stage so it is created
  1918. // at the very end (when all other parameters are configured).
  1919. // Starting stage 1. Configure sub-options and their definitions.
  1920. string config = "{ " + genIfaceConfig() + ","
  1921. "\"preferred-lifetime\": 3000,"
  1922. "\"valid-lifetime\": 4000,"
  1923. "\"rebind-timer\": 2000,"
  1924. "\"renew-timer\": 1000,"
  1925. "\"option-data\": [ {"
  1926. " \"name\": \"foo\","
  1927. " \"space\": \"isc\","
  1928. " \"data\": \"1234\""
  1929. " },"
  1930. " {"
  1931. " \"name\": \"foo2\","
  1932. " \"space\": \"isc\","
  1933. " \"data\": \"192.168.2.1\""
  1934. " } ],"
  1935. "\"option-def\": [ {"
  1936. " \"name\": \"foo\","
  1937. " \"code\": 110,"
  1938. " \"type\": \"uint32\","
  1939. " \"space\": \"isc\""
  1940. " },"
  1941. " {"
  1942. " \"name\": \"foo2\","
  1943. " \"code\": 111,"
  1944. " \"type\": \"ipv4-address\","
  1945. " \"space\": \"isc\""
  1946. " } ]"
  1947. "}";
  1948. ConstElementPtr status;
  1949. ElementPtr json = Element::fromJSON(config);
  1950. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1951. ASSERT_TRUE(status);
  1952. checkResult(status, 0);
  1953. CfgMgr::instance().clear();
  1954. // Stage 2. Configure base option and a subnet. Please note that
  1955. // the configuration from the stage 2 is repeated because BIND
  1956. // configuration manager sends whole configuration for the lists
  1957. // where at least one element is being modified or added.
  1958. config = "{ " + genIfaceConfig() + ","
  1959. "\"preferred-lifetime\": 3000,"
  1960. "\"valid-lifetime\": 4000,"
  1961. "\"rebind-timer\": 2000,"
  1962. "\"renew-timer\": 1000,"
  1963. "\"option-data\": [ {"
  1964. " \"name\": \"base-option\","
  1965. " \"data\": \"11\""
  1966. " },"
  1967. " {"
  1968. " \"name\": \"foo\","
  1969. " \"space\": \"isc\","
  1970. " \"data\": \"1234\""
  1971. " },"
  1972. " {"
  1973. " \"name\": \"foo2\","
  1974. " \"space\": \"isc\","
  1975. " \"data\": \"192.168.2.1\""
  1976. " } ],"
  1977. "\"option-def\": [ {"
  1978. " \"name\": \"base-option\","
  1979. " \"code\": 100,"
  1980. " \"type\": \"uint8\","
  1981. " \"space\": \"dhcp6\","
  1982. " \"encapsulate\": \"isc\""
  1983. "},"
  1984. "{"
  1985. " \"name\": \"foo\","
  1986. " \"code\": 110,"
  1987. " \"type\": \"uint32\","
  1988. " \"space\": \"isc\""
  1989. " },"
  1990. " {"
  1991. " \"name\": \"foo2\","
  1992. " \"code\": 111,"
  1993. " \"type\": \"ipv4-address\","
  1994. " \"space\": \"isc\""
  1995. " } ],"
  1996. "\"subnet6\": [ { "
  1997. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  1998. " \"subnet\": \"2001:db8:1::/64\""
  1999. " } ]"
  2000. "}";
  2001. json = Element::fromJSON(config);
  2002. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2003. ASSERT_TRUE(status);
  2004. checkResult(status, 0);
  2005. // Get the subnet.
  2006. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2007. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2008. ASSERT_TRUE(subnet);
  2009. // We should have one option available.
  2010. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  2011. ASSERT_TRUE(options);
  2012. ASSERT_EQ(1, options->size());
  2013. // Get the option.
  2014. OptionDescriptor desc = subnet->getCfgOption()->get("dhcp6", 100);
  2015. EXPECT_TRUE(desc.option_);
  2016. EXPECT_EQ(100, desc.option_->getType());
  2017. // This opton should comprise two sub-options.
  2018. // Onf of them is 'foo' with code 110.
  2019. OptionPtr option_foo = desc.option_->getOption(110);
  2020. ASSERT_TRUE(option_foo);
  2021. EXPECT_EQ(110, option_foo->getType());
  2022. // ...another one 'foo2' with code 111.
  2023. OptionPtr option_foo2 = desc.option_->getOption(111);
  2024. ASSERT_TRUE(option_foo2);
  2025. EXPECT_EQ(111, option_foo2->getType());
  2026. }
  2027. // Goal of this test is to verify options configuration
  2028. // for multiple subnets.
  2029. TEST_F(Dhcp6ParserTest, optionDataInMultipleSubnets) {
  2030. ConstElementPtr x;
  2031. string config = "{ " + genIfaceConfig() + ","
  2032. "\"preferred-lifetime\": 3000,"
  2033. "\"rebind-timer\": 2000, "
  2034. "\"renew-timer\": 1000, "
  2035. "\"subnet6\": [ { "
  2036. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2037. " \"subnet\": \"2001:db8:1::/64\", "
  2038. " \"option-data\": [ {"
  2039. " \"name\": \"subscriber-id\","
  2040. " \"data\": \"0102030405060708090A\","
  2041. " \"csv-format\": False"
  2042. " } ]"
  2043. " },"
  2044. " {"
  2045. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  2046. " \"subnet\": \"2001:db8:2::/64\", "
  2047. " \"option-data\": [ {"
  2048. " \"name\": \"user-class\","
  2049. " \"data\": \"FFFEFDFCFB\","
  2050. " \"csv-format\": False"
  2051. " } ]"
  2052. " } ],"
  2053. "\"valid-lifetime\": 4000 }";
  2054. ElementPtr json = Element::fromJSON(config);
  2055. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2056. checkResult(x, 0);
  2057. Subnet6Ptr subnet1 = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2058. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2059. ASSERT_TRUE(subnet1);
  2060. OptionContainerPtr options1 = subnet1->getCfgOption()->getAll("dhcp6");
  2061. ASSERT_EQ(1, options1->size());
  2062. // Get the search index. Index #1 is to search using option code.
  2063. const OptionContainerTypeIndex& idx1 = options1->get<1>();
  2064. // Get the options for specified index. Expecting one option to be
  2065. // returned but in theory we may have multiple options with the same
  2066. // code so we get the range.
  2067. std::pair<OptionContainerTypeIndex::const_iterator,
  2068. OptionContainerTypeIndex::const_iterator> range1 =
  2069. idx1.equal_range(D6O_SUBSCRIBER_ID);
  2070. // Expect single option with the code equal to 38.
  2071. ASSERT_EQ(1, std::distance(range1.first, range1.second));
  2072. const uint8_t subid_expected[] = {
  2073. 0x01, 0x02, 0x03, 0x04, 0x05,
  2074. 0x06, 0x07, 0x08, 0x09, 0x0A
  2075. };
  2076. // Check if option is valid in terms of code and carried data.
  2077. testOption(*range1.first, D6O_SUBSCRIBER_ID, subid_expected,
  2078. sizeof(subid_expected));
  2079. // Test another subnet in the same way.
  2080. Subnet6Ptr subnet2 = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2081. selectSubnet(IOAddress("2001:db8:2::4"), classify_);
  2082. ASSERT_TRUE(subnet2);
  2083. OptionContainerPtr options2 = subnet2->getCfgOption()->getAll("dhcp6");
  2084. ASSERT_EQ(1, options2->size());
  2085. const OptionContainerTypeIndex& idx2 = options2->get<1>();
  2086. std::pair<OptionContainerTypeIndex::const_iterator,
  2087. OptionContainerTypeIndex::const_iterator> range2 =
  2088. idx2.equal_range(D6O_USER_CLASS);
  2089. ASSERT_EQ(1, std::distance(range2.first, range2.second));
  2090. const uint8_t user_class_expected[] = {
  2091. 0xFF, 0xFE, 0xFD, 0xFC, 0xFB
  2092. };
  2093. testOption(*range2.first, D6O_USER_CLASS, user_class_expected,
  2094. sizeof(user_class_expected));
  2095. }
  2096. // The goal of this test is to check that the option carrying a boolean
  2097. // value can be configured using one of the values: "true", "false", "0"
  2098. // or "1".
  2099. TEST_F(Dhcp6ParserTest, optionDataBoolean) {
  2100. // Create configuration. Use standard option 1000.
  2101. std::map<std::string, std::string> params;
  2102. params["name"] = "bool-option";
  2103. params["space"] = "dhcp6";
  2104. params["code"] = "1000";
  2105. params["data"] = "true";
  2106. params["csv-format"] = "true";
  2107. std::string config = createConfigWithOption(params);
  2108. ASSERT_TRUE(executeConfiguration(config, "parse configuration with a"
  2109. " boolean value"));
  2110. // The subnet should now hold one option with the code 1000.
  2111. OptionDescriptor desc =
  2112. getOptionFromSubnet(IOAddress("2001:db8:1::5"), 1000);
  2113. ASSERT_TRUE(desc.option_);
  2114. // This option should be set to "true", represented as 0x1 in the option
  2115. // buffer.
  2116. uint8_t expected_option_data[] = {
  2117. 0x1
  2118. };
  2119. testConfiguration(params, 1000, expected_option_data,
  2120. sizeof(expected_option_data));
  2121. // Configure the option with the "1" value. This should have the same
  2122. // effect as if "true" was specified.
  2123. params["data"] = "1";
  2124. testConfiguration(params, 1000, expected_option_data,
  2125. sizeof(expected_option_data));
  2126. // The value of "1" with a few leading zeros should work too.
  2127. params["data"] = "00001";
  2128. testConfiguration(params, 1000, expected_option_data,
  2129. sizeof(expected_option_data));
  2130. // Configure the option with the "false" value.
  2131. params["data"] = "false";
  2132. // The option buffer should now hold the value of 0.
  2133. expected_option_data[0] = 0;
  2134. testConfiguration(params, 1000, expected_option_data,
  2135. sizeof(expected_option_data));
  2136. // Specifying "0" should have the same effect as "false".
  2137. params["data"] = "0";
  2138. testConfiguration(params, 1000, expected_option_data,
  2139. sizeof(expected_option_data));
  2140. // The same effect should be for multiple 0 chars.
  2141. params["data"] = "00000";
  2142. testConfiguration(params, 1000, expected_option_data,
  2143. sizeof(expected_option_data));
  2144. // Bogus values should not be accepted.
  2145. params["data"] = "bugus";
  2146. testInvalidOptionParam(params);
  2147. params["data"] = "2";
  2148. testInvalidOptionParam(params);
  2149. // Now let's test that it is possible to use binary format.
  2150. params["data"] = "0";
  2151. params["csv-format"] = "false";
  2152. testConfiguration(params, 1000, expected_option_data,
  2153. sizeof(expected_option_data));
  2154. // The binary 1 should work as well.
  2155. params["data"] = "1";
  2156. expected_option_data[0] = 1;
  2157. testConfiguration(params, 1000, expected_option_data,
  2158. sizeof(expected_option_data));
  2159. // As well as an even number of digits.
  2160. params["data"] = "01";
  2161. testConfiguration(params, 1000, expected_option_data,
  2162. sizeof(expected_option_data));
  2163. }
  2164. // Verify that empty option name is rejected in the configuration.
  2165. TEST_F(Dhcp6ParserTest, optionNameEmpty) {
  2166. // Empty option names not allowed.
  2167. testInvalidOptionParam("", "name");
  2168. }
  2169. // Verify that empty option name with spaces is rejected
  2170. // in the configuration.
  2171. TEST_F(Dhcp6ParserTest, optionNameSpaces) {
  2172. // Spaces in option names not allowed.
  2173. testInvalidOptionParam("option foo", "name");
  2174. }
  2175. // Verify that negative option code is rejected in the configuration.
  2176. TEST_F(Dhcp6ParserTest, optionCodeNegative) {
  2177. // Check negative option code -4. This should fail too.
  2178. testInvalidOptionParam("-4", "code");
  2179. }
  2180. // Verify that out of bounds option code is rejected in the configuration.
  2181. TEST_F(Dhcp6ParserTest, optionCodeNonUint16) {
  2182. // The valid option codes are uint16_t values so passing
  2183. // uint16_t maximum value incremented by 1 should result
  2184. // in failure.
  2185. testInvalidOptionParam("65536", "code");
  2186. }
  2187. // Verify that out of bounds option code is rejected in the configuration.
  2188. TEST_F(Dhcp6ParserTest, optionCodeHighNonUint16) {
  2189. // Another check for uint16_t overflow but this time
  2190. // let's pass even greater option code value.
  2191. testInvalidOptionParam("70000", "code");
  2192. }
  2193. // Verify that zero option code is rejected in the configuration.
  2194. TEST_F(Dhcp6ParserTest, optionCodeZero) {
  2195. // Option code 0 is reserved and should not be accepted
  2196. // by configuration parser.
  2197. testInvalidOptionParam("0", "code");
  2198. }
  2199. // Verify that option data which contains non hexadecimal characters
  2200. // is rejected by the configuration.
  2201. TEST_F(Dhcp6ParserTest, optionDataInvalidChar) {
  2202. // Option code 0 is reserved and should not be accepted
  2203. // by configuration parser.
  2204. testInvalidOptionParam("01020R", "data");
  2205. }
  2206. // Verify that option data containing '0x' prefix is rejected
  2207. // by the configuration.
  2208. TEST_F(Dhcp6ParserTest, optionDataUnexpectedPrefix) {
  2209. // Option code 0 is reserved and should not be accepted
  2210. // by configuration parser.
  2211. testInvalidOptionParam("0x0102", "data");
  2212. }
  2213. // Verify that either lower or upper case characters are allowed
  2214. // to specify the option data.
  2215. TEST_F(Dhcp6ParserTest, optionDataLowerCase) {
  2216. ConstElementPtr x;
  2217. std::string config = createConfigWithOption("0a0b0C0D", "data");
  2218. ElementPtr json = Element::fromJSON(config);
  2219. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2220. checkResult(x, 0);
  2221. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2222. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2223. ASSERT_TRUE(subnet);
  2224. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  2225. ASSERT_EQ(1, options->size());
  2226. // Get the search index. Index #1 is to search using option code.
  2227. const OptionContainerTypeIndex& idx = options->get<1>();
  2228. // Get the options for specified index. Expecting one option to be
  2229. // returned but in theory we may have multiple options with the same
  2230. // code so we get the range.
  2231. std::pair<OptionContainerTypeIndex::const_iterator,
  2232. OptionContainerTypeIndex::const_iterator> range =
  2233. idx.equal_range(D6O_SUBSCRIBER_ID);
  2234. // Expect single option with the code equal to 38.
  2235. ASSERT_EQ(1, std::distance(range.first, range.second));
  2236. const uint8_t subid_expected[] = {
  2237. 0x0A, 0x0B, 0x0C, 0x0D
  2238. };
  2239. // Check if option is valid in terms of code and carried data.
  2240. testOption(*range.first, D6O_SUBSCRIBER_ID, subid_expected,
  2241. sizeof(subid_expected));
  2242. }
  2243. // Verify that specific option object is returned for standard
  2244. // option which has dedicated option class derived from Option.
  2245. TEST_F(Dhcp6ParserTest, stdOptionData) {
  2246. ConstElementPtr x;
  2247. std::map<std::string, std::string> params;
  2248. params["name"] = "ia-na";
  2249. params["space"] = "dhcp6";
  2250. // Option code 3 means OPTION_IA_NA.
  2251. params["code"] = "3";
  2252. params["data"] = "12345, 6789, 1516";
  2253. params["csv-format"] = "True";
  2254. std::string config = createConfigWithOption(params);
  2255. ElementPtr json = Element::fromJSON(config);
  2256. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2257. checkResult(x, 0);
  2258. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2259. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2260. ASSERT_TRUE(subnet);
  2261. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  2262. ASSERT_EQ(1, options->size());
  2263. // Get the search index. Index #1 is to search using option code.
  2264. const OptionContainerTypeIndex& idx = options->get<1>();
  2265. // Get the options for specified index. Expecting one option to be
  2266. // returned but in theory we may have multiple options with the same
  2267. // code so we get the range.
  2268. std::pair<OptionContainerTypeIndex::const_iterator,
  2269. OptionContainerTypeIndex::const_iterator> range =
  2270. idx.equal_range(D6O_IA_NA);
  2271. // Expect single option with the code equal to IA_NA option code.
  2272. ASSERT_EQ(1, std::distance(range.first, range.second));
  2273. // The actual pointer to the option is held in the option field
  2274. // in the structure returned.
  2275. OptionPtr option = range.first->option_;
  2276. ASSERT_TRUE(option);
  2277. // Option object returned for here is expected to be Option6IA
  2278. // which is derived from Option. This class is dedicated to
  2279. // represent standard option IA_NA.
  2280. boost::shared_ptr<Option6IA> optionIA =
  2281. boost::dynamic_pointer_cast<Option6IA>(option);
  2282. // If cast is unsuccessful than option returned was of a
  2283. // different type than Option6IA. This is wrong.
  2284. ASSERT_TRUE(optionIA);
  2285. // If cast was successful we may use accessors exposed by
  2286. // Option6IA to validate that the content of this option
  2287. // has been set correctly.
  2288. EXPECT_EQ(12345, optionIA->getIAID());
  2289. EXPECT_EQ(6789, optionIA->getT1());
  2290. EXPECT_EQ(1516, optionIA->getT2());
  2291. }
  2292. // This test checks if vendor options can be specified in the config file
  2293. // (in hex format), and later retrieved from configured subnet
  2294. TEST_F(Dhcp6ParserTest, vendorOptionsHex) {
  2295. // This configuration string is to configure two options
  2296. // sharing the code 1 and belonging to the different vendor spaces.
  2297. // (different vendor-id values).
  2298. string config = "{ " + genIfaceConfig() + ","
  2299. "\"preferred-lifetime\": 3000,"
  2300. "\"valid-lifetime\": 4000,"
  2301. "\"rebind-timer\": 2000,"
  2302. "\"renew-timer\": 1000,"
  2303. "\"option-data\": [ {"
  2304. " \"name\": \"option-one\","
  2305. " \"space\": \"vendor-4491\","
  2306. " \"code\": 100,"
  2307. " \"data\": \"ABCDEF0105\","
  2308. " \"csv-format\": False"
  2309. " },"
  2310. " {"
  2311. " \"name\": \"option-two\","
  2312. " \"space\": \"vendor-1234\","
  2313. " \"code\": 100,"
  2314. " \"data\": \"1234\","
  2315. " \"csv-format\": False"
  2316. " } ],"
  2317. "\"subnet6\": [ { "
  2318. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2319. " \"subnet\": \"2001:db8:1::/64\""
  2320. " } ]"
  2321. "}";
  2322. ConstElementPtr status;
  2323. ElementPtr json = Element::fromJSON(config);
  2324. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2325. ASSERT_TRUE(status);
  2326. checkResult(status, 0);
  2327. // Options should be now available for the subnet.
  2328. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2329. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2330. ASSERT_TRUE(subnet);
  2331. // Try to get the option from the vendor space 4491
  2332. OptionDescriptor desc1 = subnet->getCfgOption()->get(4491, 100);
  2333. ASSERT_TRUE(desc1.option_);
  2334. EXPECT_EQ(100, desc1.option_->getType());
  2335. // Try to get the option from the vendor space 1234
  2336. OptionDescriptor desc2 = subnet->getCfgOption()->get(1234, 100);
  2337. ASSERT_TRUE(desc2.option_);
  2338. EXPECT_EQ(100, desc1.option_->getType());
  2339. // Try to get the non-existing option from the non-existing
  2340. // option space and expect that option is not returned.
  2341. OptionDescriptor desc3 = subnet->getCfgOption()->get(5678, 38);
  2342. ASSERT_FALSE(desc3.option_);
  2343. }
  2344. // This test checks if vendor options can be specified in the config file,
  2345. // (in csv format), and later retrieved from configured subnet
  2346. TEST_F(Dhcp6ParserTest, vendorOptionsCsv) {
  2347. // This configuration string is to configure two options
  2348. // sharing the code 1 and belonging to the different vendor spaces.
  2349. // (different vendor-id values).
  2350. string config = "{ " + genIfaceConfig() + ","
  2351. "\"preferred-lifetime\": 3000,"
  2352. "\"valid-lifetime\": 4000,"
  2353. "\"rebind-timer\": 2000,"
  2354. "\"renew-timer\": 1000,"
  2355. "\"option-data\": [ {"
  2356. " \"name\": \"foo\","
  2357. " \"space\": \"vendor-4491\","
  2358. " \"code\": 100,"
  2359. " \"data\": \"this is a string vendor-opt\""
  2360. " } ],"
  2361. "\"option-def\": [ {"
  2362. " \"name\": \"foo\","
  2363. " \"code\": 100,"
  2364. " \"type\": \"string\","
  2365. " \"space\": \"vendor-4491\""
  2366. " } ],"
  2367. "\"subnet6\": [ { "
  2368. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2369. " \"subnet\": \"2001:db8:1::/64\""
  2370. " } ]"
  2371. "}";
  2372. ConstElementPtr status;
  2373. ElementPtr json = Element::fromJSON(config);
  2374. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2375. ASSERT_TRUE(status);
  2376. checkResult(status, 0);
  2377. // Options should be now available for the subnet.
  2378. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2379. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2380. ASSERT_TRUE(subnet);
  2381. // Try to get the option from the vendor space 4491
  2382. OptionDescriptor desc1 = subnet->getCfgOption()->get(4491, 100);
  2383. ASSERT_TRUE(desc1.option_);
  2384. EXPECT_EQ(100, desc1.option_->getType());
  2385. // Try to get the non-existing option from the non-existing
  2386. // option space and expect that option is not returned.
  2387. OptionDescriptor desc2 = subnet->getCfgOption()->get(5678, 100);
  2388. ASSERT_FALSE(desc2.option_);
  2389. }
  2390. /// @todo add tests similar to vendorOptionsCsv and vendorOptionsHex, but for
  2391. /// vendor options defined in a subnet.
  2392. // The goal of this test is to verify that the standard option can
  2393. // be configured to encapsulate multiple other options.
  2394. /// @todo This test is currently disabled because it relies on the option
  2395. /// 17 which is treated differently than all other options. There are no
  2396. /// other standard options used by Kea which would encapsulate other
  2397. /// options and for which values could be configured here.
  2398. TEST_F(Dhcp6ParserTest, DISABLED_stdOptionDataEncapsulate) {
  2399. // The configuration is two stage process in this test.
  2400. // In the first stage we create definitions of suboptions
  2401. // that we will add to the base option.
  2402. // Let's create some dummy options: foo and foo2.
  2403. string config = "{ " + genIfaceConfig() + ","
  2404. "\"preferred-lifetime\": 3000,"
  2405. "\"valid-lifetime\": 4000,"
  2406. "\"rebind-timer\": 2000,"
  2407. "\"renew-timer\": 1000,"
  2408. "\"option-data\": [ {"
  2409. " \"name\": \"foo\","
  2410. " \"space\": \"vendor-opts-space\","
  2411. " \"data\": \"1234\""
  2412. " },"
  2413. " {"
  2414. " \"name\": \"foo2\","
  2415. " \"space\": \"vendor-opts-space\","
  2416. " \"data\": \"192.168.2.1\""
  2417. " } ],"
  2418. "\"option-def\": [ {"
  2419. " \"name\": \"foo\","
  2420. " \"code\": 110,"
  2421. " \"type\": \"uint32\","
  2422. " \"space\": \"vendor-opts-space\""
  2423. " },"
  2424. " {"
  2425. " \"name\": \"foo2\","
  2426. " \"code\": 111,"
  2427. " \"type\": \"ipv4-address\","
  2428. " \"space\": \"vendor-opts-space\""
  2429. " } ]"
  2430. "}";
  2431. ConstElementPtr status;
  2432. ElementPtr json = Element::fromJSON(config);
  2433. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2434. ASSERT_TRUE(status);
  2435. checkResult(status, 0);
  2436. CfgMgr::instance().clear();
  2437. // Once the definitions have been added we can configure the
  2438. // standard option #17. This option comprises an enterprise
  2439. // number and sub options. By convention (introduced in
  2440. // std_option_defs.h) option named 'vendor-opts'
  2441. // encapsulates the option space named 'vendor-opts-space'.
  2442. // We add our dummy options to this option space and thus
  2443. // they should be included as sub-options in the 'vendor-opts'
  2444. // option.
  2445. config = "{ " + genIfaceConfig() + ","
  2446. "\"rebind-timer\": 2000,"
  2447. "\"renew-timer\": 1000,"
  2448. "\"option-data\": [ {"
  2449. " \"name\": \"vendor-opts\","
  2450. " \"data\": \"1234\""
  2451. " },"
  2452. " {"
  2453. " \"name\": \"foo\","
  2454. " \"space\": \"vendor-opts-space\","
  2455. " \"data\": \"1234\""
  2456. " },"
  2457. " {"
  2458. " \"name\": \"foo2\","
  2459. " \"space\": \"vendor-opts-space\","
  2460. " \"data\": \"192.168.2.1\""
  2461. " } ],"
  2462. "\"option-def\": [ {"
  2463. " \"name\": \"foo\","
  2464. " \"code\": 110,"
  2465. " \"type\": \"uint32\","
  2466. " \"space\": \"vendor-opts-space\""
  2467. " },"
  2468. " {"
  2469. " \"name\": \"foo2\","
  2470. " \"code\": 111,"
  2471. " \"type\": \"ipv4-address\","
  2472. " \"space\": \"vendor-opts-space\""
  2473. " } ],"
  2474. "\"subnet6\": [ { "
  2475. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2476. " \"subnet\": \"2001:db8:1::/64\""
  2477. " } ]"
  2478. "}";
  2479. json = Element::fromJSON(config);
  2480. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2481. ASSERT_TRUE(status);
  2482. checkResult(status, 0);
  2483. // Get the subnet.
  2484. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2485. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2486. ASSERT_TRUE(subnet);
  2487. // We should have one option available.
  2488. OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
  2489. ASSERT_TRUE(options);
  2490. ASSERT_EQ(1, options->size());
  2491. // Get the option.
  2492. OptionDescriptor desc = subnet->getCfgOption()->get("dhcp6", D6O_VENDOR_OPTS);
  2493. EXPECT_TRUE(desc.option_);
  2494. EXPECT_EQ(D6O_VENDOR_OPTS, desc.option_->getType());
  2495. // Option with the code 110 should be added as a sub-option.
  2496. OptionPtr option_foo = desc.option_->getOption(110);
  2497. ASSERT_TRUE(option_foo);
  2498. EXPECT_EQ(110, option_foo->getType());
  2499. // This option comprises a single uint32_t value thus it is
  2500. // represented by OptionInt<uint32_t> class. Let's get the
  2501. // object of this type.
  2502. boost::shared_ptr<OptionInt<uint32_t> > option_foo_uint32 =
  2503. boost::dynamic_pointer_cast<OptionInt<uint32_t> >(option_foo);
  2504. ASSERT_TRUE(option_foo_uint32);
  2505. // Validate the value according to the configuration.
  2506. EXPECT_EQ(1234, option_foo_uint32->getValue());
  2507. // Option with the code 111 should be added as a sub-option.
  2508. OptionPtr option_foo2 = desc.option_->getOption(111);
  2509. ASSERT_TRUE(option_foo2);
  2510. EXPECT_EQ(111, option_foo2->getType());
  2511. // This option comprises the IPV4 address. Such option is
  2512. // represented by OptionCustom object.
  2513. OptionCustomPtr option_foo2_v4 =
  2514. boost::dynamic_pointer_cast<OptionCustom>(option_foo2);
  2515. ASSERT_TRUE(option_foo2_v4);
  2516. // Get the IP address carried by this option and validate it.
  2517. EXPECT_EQ("192.168.2.1", option_foo2_v4->readAddress().toText());
  2518. // Option with the code 112 should not be added.
  2519. EXPECT_FALSE(desc.option_->getOption(112));
  2520. }
  2521. // Tests of the hooks libraries configuration. All tests have the pre-
  2522. // condition (checked in the test fixture's SetUp() method) that no hooks
  2523. // libraries are loaded at the start of the tests.
  2524. // Helper function to return a configuration containing an arbitrary number
  2525. // of hooks libraries.
  2526. std::string
  2527. buildHooksLibrariesConfig(const std::vector<std::string>& libraries) {
  2528. const string quote("\"");
  2529. // Create the first part of the configuration string.
  2530. string config =
  2531. "{ \"interfaces-config\": { },"
  2532. "\"hooks-libraries\": [";
  2533. // Append the libraries (separated by commas if needed)
  2534. for (int i = 0; i < libraries.size(); ++i) {
  2535. if (i > 0) {
  2536. config += string(", ");
  2537. }
  2538. config += (quote + libraries[i] + quote);
  2539. }
  2540. // Append the remainder of the configuration.
  2541. config += string(
  2542. "],"
  2543. "\"rebind-timer\": 2000,"
  2544. "\"renew-timer\": 1000,"
  2545. "\"option-data\": [ {"
  2546. " \"name\": \"foo\","
  2547. " \"space\": \"vendor-opts-space\","
  2548. " \"data\": \"1234\""
  2549. " },"
  2550. " {"
  2551. " \"name\": \"foo2\","
  2552. " \"space\": \"vendor-opts-space\","
  2553. " \"data\": \"192.168.2.1\""
  2554. " } ],"
  2555. "\"option-def\": [ {"
  2556. " \"name\": \"foo\","
  2557. " \"code\": 110,"
  2558. " \"type\": \"uint32\","
  2559. " \"space\": \"vendor-opts-space\""
  2560. " },"
  2561. " {"
  2562. " \"name\": \"foo2\","
  2563. " \"code\": 111,"
  2564. " \"type\": \"ipv4-address\","
  2565. " \"space\": \"vendor-opts-space\""
  2566. " } ]"
  2567. "}");
  2568. return (config);
  2569. }
  2570. // Convenience function for creating hooks library configuration with one or
  2571. // two character string constants.
  2572. std::string
  2573. buildHooksLibrariesConfig(const char* library1 = NULL,
  2574. const char* library2 = NULL) {
  2575. std::vector<std::string> libraries;
  2576. if (library1 != NULL) {
  2577. libraries.push_back(string(library1));
  2578. if (library2 != NULL) {
  2579. libraries.push_back(string(library2));
  2580. }
  2581. }
  2582. return (buildHooksLibrariesConfig(libraries));
  2583. }
  2584. // The goal of this test is to verify the configuration of hooks libraries if
  2585. // none are specified.
  2586. TEST_F(Dhcp6ParserTest, NoHooksLibraries) {
  2587. // Parse a configuration containing no names.
  2588. string config = buildHooksLibrariesConfig();
  2589. if (!executeConfiguration(config,
  2590. "set configuration with no hooks libraries")) {
  2591. FAIL() << "Unable to execute configuration";
  2592. } else {
  2593. // No libraries should be loaded at the end of the test.
  2594. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  2595. EXPECT_TRUE(libraries.empty());
  2596. }
  2597. }
  2598. // Verify parsing fails with one library that will fail validation.
  2599. TEST_F(Dhcp6ParserTest, InvalidLibrary) {
  2600. // Parse a configuration containing a failing library.
  2601. string config = buildHooksLibrariesConfig(NOT_PRESENT_LIBRARY);
  2602. ConstElementPtr status;
  2603. ElementPtr json = Element::fromJSON(config);
  2604. ASSERT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2605. // The status object must not be NULL
  2606. ASSERT_TRUE(status);
  2607. // Returned value should not be 0
  2608. comment_ = parseAnswer(rcode_, status);
  2609. EXPECT_NE(0, rcode_);
  2610. }
  2611. // Verify the configuration of hooks libraries with two being specified.
  2612. TEST_F(Dhcp6ParserTest, LibrariesSpecified) {
  2613. // Marker files should not be present.
  2614. EXPECT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE));
  2615. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  2616. // Set up the configuration with two libraries and load them.
  2617. string config = buildHooksLibrariesConfig(CALLOUT_LIBRARY_1,
  2618. CALLOUT_LIBRARY_2);
  2619. ASSERT_TRUE(executeConfiguration(config,
  2620. "load two valid libraries"));
  2621. // Expect two libraries to be loaded in the correct order (load marker file
  2622. // is present, no unload marker file).
  2623. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  2624. ASSERT_EQ(2, libraries.size());
  2625. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  2626. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  2627. CfgMgr::instance().commit();
  2628. // Unload the libraries. The load file should not have changed, but
  2629. // the unload one should indicate the unload() functions have been run.
  2630. config = buildHooksLibrariesConfig();
  2631. ASSERT_TRUE(executeConfiguration(config, "unloading libraries"));
  2632. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  2633. EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "21"));
  2634. // Expect the hooks system to say that none are loaded.
  2635. libraries = HooksManager::getLibraryNames();
  2636. EXPECT_TRUE(libraries.empty());
  2637. }
  2638. // This test verifies that it is possible to select subset of interfaces on
  2639. // which server should listen.
  2640. TEST_F(Dhcp6ParserTest, selectedInterfaces) {
  2641. IfaceMgrTestConfig test_config(true);
  2642. // Make sure there is no garbage interface configuration in the CfgMgr.
  2643. ASSERT_FALSE(test_config.socketOpen("eth0", AF_INET6));
  2644. ASSERT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  2645. ConstElementPtr status;
  2646. string config = "{ \"interfaces-config\": {"
  2647. " \"interfaces\": [ \"eth0\" ]"
  2648. "},"
  2649. "\"preferred-lifetime\": 3000,"
  2650. "\"rebind-timer\": 2000, "
  2651. "\"renew-timer\": 1000, "
  2652. "\"valid-lifetime\": 4000 }";
  2653. ElementPtr json = Element::fromJSON(config);
  2654. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2655. // returned value must be 1 (values error)
  2656. // as the pool does not belong to that subnet
  2657. checkResult(status, 0);
  2658. CfgMgr::instance().getStagingCfg()->getCfgIface()->openSockets(AF_INET6, 10000);
  2659. // eth0 and eth1 were explicitly selected. eth2 was not.
  2660. EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET6));
  2661. EXPECT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  2662. }
  2663. // This test verifies that it is possible to configure the server to listen on
  2664. // all interfaces.
  2665. TEST_F(Dhcp6ParserTest, allInterfaces) {
  2666. IfaceMgrTestConfig test_config(true);
  2667. ASSERT_FALSE(test_config.socketOpen("eth0", AF_INET6));
  2668. ASSERT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  2669. ConstElementPtr status;
  2670. // This configuration specifies two interfaces on which server should listen
  2671. // but also includes '*'. This keyword switches server into the
  2672. // mode when it listens on all interfaces regardless of what interface names
  2673. // were specified in the "interfaces" parameter.
  2674. string config = "{ \"interfaces-config\": {"
  2675. " \"interfaces\": [ \"eth0\", \"eth1\", \"*\" ]"
  2676. "},"
  2677. "\"preferred-lifetime\": 3000,"
  2678. "\"rebind-timer\": 2000, "
  2679. "\"renew-timer\": 1000, "
  2680. "\"valid-lifetime\": 4000 }";
  2681. ElementPtr json = Element::fromJSON(config);
  2682. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2683. checkResult(status, 0);
  2684. CfgMgr::instance().getStagingCfg()->getCfgIface()->openSockets(AF_INET6, 10000);
  2685. // All interfaces should be now active.
  2686. EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET6));
  2687. EXPECT_TRUE(test_config.socketOpen("eth1", AF_INET6));
  2688. }
  2689. // This test checks if it is possible to specify relay information
  2690. TEST_F(Dhcp6ParserTest, subnetRelayInfo) {
  2691. ConstElementPtr status;
  2692. // A config with relay information.
  2693. string config = "{ " + genIfaceConfig() + ","
  2694. "\"rebind-timer\": 2000, "
  2695. "\"renew-timer\": 1000, "
  2696. "\"subnet6\": [ { "
  2697. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  2698. " \"relay\": { "
  2699. " \"ip-address\": \"2001:db8:1::abcd\""
  2700. " },"
  2701. " \"subnet\": \"2001:db8:1::/64\" } ],"
  2702. "\"preferred-lifetime\": 3000, "
  2703. "\"valid-lifetime\": 4000 }";
  2704. ElementPtr json = Element::fromJSON(config);
  2705. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2706. // returned value should be 0 (configuration success)
  2707. checkResult(status, 0);
  2708. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2709. selectSubnet(IOAddress("2001:db8:1::1"), classify_);
  2710. ASSERT_TRUE(subnet);
  2711. EXPECT_EQ("2001:db8:1::abcd", subnet->getRelayInfo().addr_.toText());
  2712. }
  2713. // Goal of this test is to verify that multiple subnets can be configured
  2714. // with defined client classes.
  2715. TEST_F(Dhcp6ParserTest, classifySubnets) {
  2716. ConstElementPtr x;
  2717. string config = "{ " + genIfaceConfig() + ","
  2718. "\"preferred-lifetime\": 3000,"
  2719. "\"rebind-timer\": 2000, "
  2720. "\"renew-timer\": 1000, "
  2721. "\"subnet6\": [ { "
  2722. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2723. " \"subnet\": \"2001:db8:1::/64\", "
  2724. " \"client-class\": \"alpha\" "
  2725. " },"
  2726. " {"
  2727. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  2728. " \"subnet\": \"2001:db8:2::/64\", "
  2729. " \"client-class\": \"beta\" "
  2730. " },"
  2731. " {"
  2732. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  2733. " \"subnet\": \"2001:db8:3::/64\", "
  2734. " \"client-class\": \"gamma\" "
  2735. " },"
  2736. " {"
  2737. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  2738. " \"subnet\": \"2001:db8:4::/64\" "
  2739. " } ],"
  2740. "\"valid-lifetime\": 4000 }";
  2741. ElementPtr json = Element::fromJSON(config);
  2742. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2743. checkResult(x, 0);
  2744. const Subnet6Collection* subnets =
  2745. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  2746. ASSERT_TRUE(subnets);
  2747. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  2748. // Let's check if client belonging to alpha class is supported in subnet[0]
  2749. // and not supported in any other subnet (except subnet[3], which allows
  2750. // everyone).
  2751. ClientClasses classes;
  2752. classes.insert("alpha");
  2753. EXPECT_TRUE (subnets->at(0)->clientSupported(classes));
  2754. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2755. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2756. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2757. // Let's check if client belonging to beta class is supported in subnet[1]
  2758. // and not supported in any other subnet (except subnet[3], which allows
  2759. // everyone).
  2760. classes.clear();
  2761. classes.insert("beta");
  2762. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2763. EXPECT_TRUE (subnets->at(1)->clientSupported(classes));
  2764. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2765. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2766. // Let's check if client belonging to gamma class is supported in subnet[2]
  2767. // and not supported in any other subnet (except subnet[3], which allows
  2768. // everyone).
  2769. classes.clear();
  2770. classes.insert("gamma");
  2771. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2772. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2773. EXPECT_TRUE (subnets->at(2)->clientSupported(classes));
  2774. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2775. // Let's check if client belonging to some other class (not mentioned in
  2776. // the config) is supported only in subnet[3], which allows everyone.
  2777. classes.clear();
  2778. classes.insert("delta");
  2779. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2780. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2781. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2782. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2783. // Finally, let's check class-less client. He should be allowed only in
  2784. // the last subnet, which does not have any class restrictions.
  2785. classes.clear();
  2786. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2787. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2788. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2789. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2790. }
  2791. // This test checks the ability of the server to parse a configuration
  2792. // containing a full, valid dhcp-ddns (D2ClientConfig) entry.
  2793. TEST_F(Dhcp6ParserTest, d2ClientConfig) {
  2794. ConstElementPtr status;
  2795. // Verify that the D2 configuraiton can be fetched and is set to disabled.
  2796. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2797. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  2798. // Verify that the convenience method agrees.
  2799. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  2800. string config_str = "{ " + genIfaceConfig() + ","
  2801. "\"preferred-lifetime\": 3000,"
  2802. "\"valid-lifetime\": 4000,"
  2803. "\"rebind-timer\": 2000, "
  2804. "\"renew-timer\": 1000, "
  2805. "\"subnet6\": [ { "
  2806. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2807. " \"subnet\": \"2001:db8:1::/64\" } ], "
  2808. " \"dhcp-ddns\" : {"
  2809. " \"enable-updates\" : true, "
  2810. " \"server-ip\" : \"3001::1\", "
  2811. " \"server-port\" : 777, "
  2812. " \"sender-ip\" : \"3001::2\", "
  2813. " \"sender-port\" : 778, "
  2814. " \"max-queue-size\" : 2048, "
  2815. " \"ncr-protocol\" : \"UDP\", "
  2816. " \"ncr-format\" : \"JSON\", "
  2817. " \"always-include-fqdn\" : true, "
  2818. " \"allow-client-update\" : true, "
  2819. " \"override-no-update\" : true, "
  2820. " \"override-client-update\" : true, "
  2821. " \"replace-client-name\" : true, "
  2822. " \"generated-prefix\" : \"test.prefix\", "
  2823. " \"qualifying-suffix\" : \"test.suffix.\" },"
  2824. "\"valid-lifetime\": 4000 }";
  2825. // Convert the JSON string to configuration elements.
  2826. ElementPtr config;
  2827. ASSERT_NO_THROW(config = Element::fromJSON(config_str));
  2828. // Pass the configuration in for parsing.
  2829. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config));
  2830. // check if returned status is OK
  2831. checkResult(status, 0);
  2832. // Verify that DHCP-DDNS updating is enabled.
  2833. EXPECT_TRUE(CfgMgr::instance().ddnsEnabled());
  2834. // Verify that the D2 configuration can be retrieved.
  2835. d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2836. ASSERT_TRUE(d2_client_config);
  2837. // Verify that the configuration values are correct.
  2838. EXPECT_TRUE(d2_client_config->getEnableUpdates());
  2839. EXPECT_EQ("3001::1", d2_client_config->getServerIp().toText());
  2840. EXPECT_EQ(777, d2_client_config->getServerPort());
  2841. EXPECT_EQ("3001::2", d2_client_config->getSenderIp().toText());
  2842. EXPECT_EQ(778, d2_client_config->getSenderPort());
  2843. EXPECT_EQ(2048, d2_client_config->getMaxQueueSize());
  2844. EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_client_config->getNcrProtocol());
  2845. EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_client_config->getNcrFormat());
  2846. EXPECT_TRUE(d2_client_config->getAlwaysIncludeFqdn());
  2847. EXPECT_TRUE(d2_client_config->getOverrideNoUpdate());
  2848. EXPECT_TRUE(d2_client_config->getOverrideClientUpdate());
  2849. EXPECT_TRUE(d2_client_config->getReplaceClientName());
  2850. EXPECT_EQ("test.prefix", d2_client_config->getGeneratedPrefix());
  2851. EXPECT_EQ("test.suffix.", d2_client_config->getQualifyingSuffix());
  2852. }
  2853. // This test checks the ability of the server to handle a configuration
  2854. // containing an invalid dhcp-ddns (D2ClientConfig) entry.
  2855. TEST_F(Dhcp6ParserTest, invalidD2ClientConfig) {
  2856. ConstElementPtr status;
  2857. // Configuration string with an invalid D2 client config,
  2858. // "server-ip" is invalid.
  2859. string config_str = "{ " + genIfaceConfig() + ","
  2860. "\"rebind-timer\": 2000, "
  2861. "\"renew-timer\": 1000, "
  2862. "\"subnet6\": [ { "
  2863. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2864. " \"subnet\": \"2001:db8:1::/64\" } ], "
  2865. " \"dhcp-ddns\" : {"
  2866. " \"enable-updates\" : true, "
  2867. " \"server-ip\" : \"bogus-value\", "
  2868. " \"server-port\" : 5301, "
  2869. " \"ncr-protocol\" : \"UDP\", "
  2870. " \"ncr-format\" : \"JSON\", "
  2871. " \"always-include-fqdn\" : true, "
  2872. " \"allow-client-update\" : true, "
  2873. " \"override-no-update\" : true, "
  2874. " \"override-client-update\" : true, "
  2875. " \"replace-client-name\" : true, "
  2876. " \"generated-prefix\" : \"test.prefix\", "
  2877. " \"qualifying-suffix\" : \"test.suffix.\" },"
  2878. "\"valid-lifetime\": 4000 }";
  2879. // Convert the JSON string to configuration elements.
  2880. ElementPtr config;
  2881. ASSERT_NO_THROW(config = Element::fromJSON(config_str));
  2882. // Configuration should not throw, but should fail.
  2883. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config));
  2884. // check if returned status is failed.
  2885. checkResult(status, 1);
  2886. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  2887. // Verify that the D2 configuraiton can be fetched and is set to disabled.
  2888. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2889. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  2890. // Verify that the convenience method agrees.
  2891. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  2892. }
  2893. /// @brief Checks if the reservation is in the range of reservations.
  2894. ///
  2895. /// @param resrv Reservation to be searched for.
  2896. /// @param range Range of reservations returned by the @c Host object
  2897. /// in which the reservation will be searched.
  2898. bool reservationExists(const IPv6Resrv& resrv, const IPv6ResrvRange& range) {
  2899. for (IPv6ResrvIterator it = range.first; it != range.second;
  2900. ++it) {
  2901. if (resrv == it->second) {
  2902. return (true);
  2903. }
  2904. }
  2905. return (false);
  2906. }
  2907. // This test verifies that the host reservations can be specified for
  2908. // respective IPv6 subnets.
  2909. TEST_F(Dhcp6ParserTest, reservations) {
  2910. ConstElementPtr x;
  2911. string config = "{ " + genIfaceConfig() + ","
  2912. "\"rebind-timer\": 2000, "
  2913. "\"renew-timer\": 1000, "
  2914. "\"subnet6\": [ "
  2915. " { "
  2916. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2917. " \"subnet\": \"2001:db8:1::/64\", "
  2918. " \"id\": 123,"
  2919. " \"reservations\": ["
  2920. " ]"
  2921. " },"
  2922. " {"
  2923. " \"reservations\": ["
  2924. " {"
  2925. " \"duid\": \"01:02:03:04:05:06:07:08:09:0A\","
  2926. " \"ip-addresses\": [ \"2001:db8:2::1234\" ],"
  2927. " \"hostname\": \"\""
  2928. " },"
  2929. " {"
  2930. " \"hw-address\": \"01:02:03:04:05:06\","
  2931. " \"ip-addresses\": [ \"2001:db8:2::abcd\" ],"
  2932. " \"hostname\": \"\""
  2933. " }"
  2934. " ],"
  2935. " \"pools\": [ ],"
  2936. " \"subnet\": \"2001:db8:2::/64\", "
  2937. " \"id\": 234"
  2938. " },"
  2939. " {"
  2940. " \"pools\": [ ],"
  2941. " \"subnet\": \"2001:db8:3::/64\", "
  2942. " \"id\": 542,"
  2943. " \"reservations\": ["
  2944. " {"
  2945. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\","
  2946. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  2947. " \"hostname\": \"\""
  2948. " },"
  2949. " {"
  2950. " \"hw-address\": \"06:05:04:03:02:01\","
  2951. " \"prefixes\": [ \"2001:db8:3:1::/96\" ],"
  2952. " \"hostname\": \"\""
  2953. " }"
  2954. " ]"
  2955. " } "
  2956. "], "
  2957. "\"preferred-lifetime\": 3000,"
  2958. "\"valid-lifetime\": 4000 }";
  2959. ElementPtr json = Element::fromJSON(config);
  2960. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2961. checkResult(x, 0);
  2962. // Make sure all subnets have been successfully configured. There is no
  2963. // need to sanity check the subnet properties because it should have
  2964. // been already tested by other tests.
  2965. const Subnet6Collection* subnets =
  2966. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  2967. ASSERT_TRUE(subnets);
  2968. ASSERT_EQ(3, subnets->size());
  2969. // Hosts configuration must be available.
  2970. CfgHostsPtr hosts_cfg = CfgMgr::instance().getStagingCfg()->getCfgHosts();
  2971. ASSERT_TRUE(hosts_cfg);
  2972. // Let's create an object holding hardware address of the host having
  2973. // a reservation in the subnet having id of 234. For simplicity the
  2974. // address is a collection of numbers from 1 to 6.
  2975. std::vector<uint8_t> hwaddr_vec;
  2976. for (int i = 1; i < 7; ++i) {
  2977. hwaddr_vec.push_back(static_cast<uint8_t>(i));
  2978. }
  2979. HWAddrPtr hwaddr(new HWAddr(hwaddr_vec, HTYPE_ETHER));
  2980. // Retrieve the reservation and sanity check the address reserved.
  2981. ConstHostPtr host = hosts_cfg->get6(234, DuidPtr(), hwaddr);
  2982. ASSERT_TRUE(host);
  2983. IPv6ResrvRange resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
  2984. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  2985. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_NA,
  2986. IOAddress("2001:db8:2::abcd")),
  2987. resrv));
  2988. // This reservation should be solely assigned to the subnet 234,
  2989. // and not to other two.
  2990. EXPECT_FALSE(hosts_cfg->get6(123, DuidPtr(), hwaddr));
  2991. EXPECT_FALSE(hosts_cfg->get6(542, DuidPtr(), hwaddr));
  2992. // Do the same test for the DUID based reservation.
  2993. std::vector<uint8_t> duid_vec;
  2994. for (int i = 1; i < 0xb; ++i) {
  2995. duid_vec.push_back(static_cast<uint8_t>(i));
  2996. }
  2997. DuidPtr duid(new DUID(duid_vec));
  2998. host = hosts_cfg->get6(234, duid);
  2999. ASSERT_TRUE(host);
  3000. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
  3001. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3002. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_NA,
  3003. IOAddress("2001:db8:2::1234")),
  3004. resrv));
  3005. EXPECT_FALSE(hosts_cfg->get6(123, duid));
  3006. EXPECT_FALSE(hosts_cfg->get6(542, duid));
  3007. // The HW address used for one of the reservations in the subnet 542
  3008. // consists of numbers from 6 to 1. So, let's just reverse the order
  3009. // of the address from the previous test.
  3010. hwaddr->hwaddr_.assign(hwaddr_vec.rbegin(), hwaddr_vec.rend());
  3011. host = hosts_cfg->get6(542, DuidPtr(), hwaddr);
  3012. EXPECT_TRUE(host);
  3013. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_PD);
  3014. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3015. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_PD,
  3016. IOAddress("2001:db8:3:1::"),
  3017. 96), resrv));
  3018. // This reservation must not belong to other subnets.
  3019. EXPECT_FALSE(hosts_cfg->get6(123, DuidPtr(), hwaddr));
  3020. EXPECT_FALSE(hosts_cfg->get6(234, DuidPtr(), hwaddr));
  3021. // Repeat the test for the DUID based reservation in this subnet.
  3022. duid.reset(new DUID(std::vector<uint8_t>(duid_vec.rbegin(),
  3023. duid_vec.rend())));
  3024. host = hosts_cfg->get6(542, duid);
  3025. ASSERT_TRUE(host);
  3026. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_PD);
  3027. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3028. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_PD,
  3029. IOAddress("2001:db8:3:2::"),
  3030. 96), resrv));
  3031. EXPECT_FALSE(hosts_cfg->get6(123, duid));
  3032. EXPECT_FALSE(hosts_cfg->get6(234, duid));
  3033. }
  3034. // This test verifies that the bogus host reservation would trigger a
  3035. // server configuration error.
  3036. TEST_F(Dhcp6ParserTest, reservationBogus) {
  3037. // Case 1: misspelled "duid" parameter.
  3038. ConstElementPtr x;
  3039. string config = "{ " + genIfaceConfig() + ","
  3040. "\"rebind-timer\": 2000, "
  3041. "\"renew-timer\": 1000, "
  3042. "\"subnet6\": [ "
  3043. " { "
  3044. " \"pools\": [ ],"
  3045. " \"subnet\": \"2001:db8:3::/64\", "
  3046. " \"id\": 542,"
  3047. " \"reservations\": ["
  3048. " {"
  3049. " \"dui\": \"0A:09:08:07:06:05:04:03:02:01\","
  3050. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  3051. " \"hostname\": \"\""
  3052. " }"
  3053. " ]"
  3054. " } "
  3055. "], "
  3056. "\"preferred-lifetime\": 3000,"
  3057. "\"valid-lifetime\": 4000 }";
  3058. ElementPtr json = Element::fromJSON(config);
  3059. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3060. checkResult(x, 1);
  3061. // Case 2: DUID and HW Address both specified.
  3062. config = "{ " + genIfaceConfig() + ","
  3063. "\"rebind-timer\": 2000, "
  3064. "\"renew-timer\": 1000, "
  3065. "\"subnet6\": [ "
  3066. " { "
  3067. " \"pools\": [ ],"
  3068. " \"subnet\": \"2001:db8:3::/64\", "
  3069. " \"id\": 542,"
  3070. " \"reservations\": ["
  3071. " {"
  3072. " \"hw-address\": \"01:02:03:04:05:06\","
  3073. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\","
  3074. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  3075. " \"hostname\": \"\""
  3076. " }"
  3077. " ]"
  3078. " } "
  3079. "], "
  3080. "\"preferred-lifetime\": 3000,"
  3081. "\"valid-lifetime\": 4000 }";
  3082. json = Element::fromJSON(config);
  3083. // Remove existing configuration, if any.
  3084. CfgMgr::instance().clear();
  3085. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3086. checkResult(x, 1);
  3087. // Case 3: Neither ip address nor hostname specified.
  3088. config = "{ " + genIfaceConfig() + ","
  3089. "\"rebind-timer\": 2000, "
  3090. "\"renew-timer\": 1000, "
  3091. "\"subnet6\": [ "
  3092. " { "
  3093. " \"pools\": [ ],"
  3094. " \"subnet\": \"2001:db8:3::/64\", "
  3095. " \"id\": 542,"
  3096. " \"reservations\": ["
  3097. " {"
  3098. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\""
  3099. " }"
  3100. " ]"
  3101. " } "
  3102. "], "
  3103. "\"preferred-lifetime\": 3000,"
  3104. "\"valid-lifetime\": 4000 }";
  3105. json = Element::fromJSON(config);
  3106. // Remove existing configuration, if any.
  3107. CfgMgr::instance().clear();
  3108. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3109. checkResult(x, 1);
  3110. }
  3111. /// The goal of this test is to verify that configuration can include
  3112. /// MAC/Hardware sources. This test also checks if the aliases are
  3113. /// handled properly (rfc6939 = client-addr-relay, rfc4649 = remote-id,
  3114. /// rfc4580 = subscriber-id).
  3115. TEST_F(Dhcp6ParserTest, macSources) {
  3116. ConstElementPtr status;
  3117. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3118. Element::fromJSON("{ " + genIfaceConfig() + ","
  3119. "\"mac-sources\": [ \"rfc6939\", \"rfc4649\", \"rfc4580\","
  3120. "\"client-link-addr-option\", \"remote-id\", \"subscriber-id\"],"
  3121. "\"preferred-lifetime\": 3000,"
  3122. "\"rebind-timer\": 2000, "
  3123. "\"renew-timer\": 1000, "
  3124. "\"subnet6\": [ ], "
  3125. "\"valid-lifetime\": 4000 }")));
  3126. // returned value should be 0 (success)
  3127. checkResult(status, 0);
  3128. CfgMACSources mac_sources = CfgMgr::instance().getStagingCfg()->getMACSources().get();
  3129. ASSERT_EQ(6, mac_sources.size());
  3130. // Let's check the aliases. They should be recognized to their base methods.
  3131. EXPECT_EQ(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION, mac_sources[0]);
  3132. EXPECT_EQ(HWAddr::HWADDR_SOURCE_REMOTE_ID, mac_sources[1]);
  3133. EXPECT_EQ(HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID, mac_sources[2]);
  3134. // Let's check if the actual methods are recognized properly.
  3135. EXPECT_EQ(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION, mac_sources[3]);
  3136. EXPECT_EQ(HWAddr::HWADDR_SOURCE_REMOTE_ID, mac_sources[4]);
  3137. EXPECT_EQ(HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID, mac_sources[5]);
  3138. }
  3139. /// The goal of this test is to verify that MAC sources configuration can be
  3140. /// empty.
  3141. TEST_F(Dhcp6ParserTest, macSourcesEmpty) {
  3142. ConstElementPtr status;
  3143. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3144. Element::fromJSON("{ " + genIfaceConfig() + ","
  3145. "\"mac-sources\": [ ],"
  3146. "\"preferred-lifetime\": 3000,"
  3147. "\"rebind-timer\": 2000, "
  3148. "\"renew-timer\": 1000, "
  3149. "\"subnet6\": [ ], "
  3150. "\"valid-lifetime\": 4000 }")));
  3151. // returned value should be 0 (success)
  3152. checkResult(status, 0);
  3153. CfgMACSources mac_sources = CfgMgr::instance().getStagingCfg()->getMACSources().get();
  3154. EXPECT_EQ(0, mac_sources.size());
  3155. }
  3156. /// The goal of this test is to verify that MAC sources configuration can
  3157. /// only use valid parameters.
  3158. TEST_F(Dhcp6ParserTest, macSourcesBogus) {
  3159. ConstElementPtr status;
  3160. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3161. Element::fromJSON("{ " + genIfaceConfig() + ","
  3162. "\"mac-sources\": [ \"from-wire\" ],"
  3163. "\"preferred-lifetime\": 3000,"
  3164. "\"rebind-timer\": 2000, "
  3165. "\"renew-timer\": 1000, "
  3166. "\"subnet6\": [ ], "
  3167. "\"valid-lifetime\": 4000 }")));
  3168. // returned value should be 1 (failure)
  3169. checkResult(status, 1);
  3170. }
  3171. /// The goal of this test is to verify that Host Reservation modes can be
  3172. /// specified on a per-subnet basis.
  3173. TEST_F(Dhcp6ParserTest, hostReservationPerSubnet) {
  3174. /// - Configuration:
  3175. /// - only addresses (no prefixes)
  3176. /// - 4 subnets with:
  3177. /// - 2001:db8:1::/64 (all reservations enabled)
  3178. /// - 2001:db8:2::/64 (out-of-pool reservations)
  3179. /// - 2001:db8:3::/64 (reservations disabled)
  3180. /// - 2001:db8:3::/64 (reservations not specified)
  3181. const char* HR_CONFIG =
  3182. "{"
  3183. "\"preferred-lifetime\": 3000,"
  3184. "\"rebind-timer\": 2000, "
  3185. "\"renew-timer\": 1000, "
  3186. "\"subnet6\": [ { "
  3187. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  3188. " \"subnet\": \"2001:db8:1::/48\", "
  3189. " \"reservation-mode\": \"all\""
  3190. " },"
  3191. " {"
  3192. " \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  3193. " \"subnet\": \"2001:db8:2::/48\", "
  3194. " \"reservation-mode\": \"out-of-pool\""
  3195. " },"
  3196. " {"
  3197. " \"pools\": [ { \"pool\": \"2001:db8:3::/64\" } ],"
  3198. " \"subnet\": \"2001:db8:3::/48\", "
  3199. " \"reservation-mode\": \"disabled\""
  3200. " },"
  3201. " {"
  3202. " \"pools\": [ { \"pool\": \"2001:db8:4::/64\" } ],"
  3203. " \"subnet\": \"2001:db8:4::/48\" "
  3204. " } ],"
  3205. "\"valid-lifetime\": 4000 }";
  3206. ConstElementPtr status;
  3207. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3208. Element::fromJSON(HR_CONFIG)));
  3209. // returned value should be 0 (success)
  3210. checkResult(status, 0);
  3211. CfgMgr::instance().commit();
  3212. // Let's get all subnets and check that there are 4 of them.
  3213. ConstCfgSubnets6Ptr subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6();
  3214. ASSERT_TRUE(subnets);
  3215. const Subnet6Collection* subnet_col = subnets->getAll();
  3216. ASSERT_EQ(4, subnet_col->size()); // We expect 4 subnets
  3217. // Let's check if the parsed subnets have correct HR modes.
  3218. // Subnet 1
  3219. Subnet6Ptr subnet;
  3220. subnet = subnets->selectSubnet(IOAddress("2001:db8:1::1"));
  3221. ASSERT_TRUE(subnet);
  3222. EXPECT_EQ(Subnet::HR_ALL, subnet->getHostReservationMode());
  3223. // Subnet 2
  3224. subnet = subnets->selectSubnet(IOAddress("2001:db8:2::1"));
  3225. ASSERT_TRUE(subnet);
  3226. EXPECT_EQ(Subnet::HR_OUT_OF_POOL, subnet->getHostReservationMode());
  3227. // Subnet 3
  3228. subnet = subnets->selectSubnet(IOAddress("2001:db8:3::1"));
  3229. ASSERT_TRUE(subnet);
  3230. EXPECT_EQ(Subnet::HR_DISABLED, subnet->getHostReservationMode());
  3231. // Subnet 4
  3232. subnet = subnets->selectSubnet(IOAddress("2001:db8:4::1"));
  3233. ASSERT_TRUE(subnet);
  3234. EXPECT_EQ(Subnet::HR_ALL, subnet->getHostReservationMode());
  3235. }
  3236. /// The goal of this test is to verify that configuration can include
  3237. /// Relay Supplied options (specified as numbers).
  3238. TEST_F(Dhcp6ParserTest, rsooNumbers) {
  3239. ConstElementPtr status;
  3240. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3241. Element::fromJSON("{ " + genIfaceConfig() + ","
  3242. "\"relay-supplied-options\": [ \"10\", \"20\", \"30\" ],"
  3243. "\"preferred-lifetime\": 3000,"
  3244. "\"rebind-timer\": 2000, "
  3245. "\"renew-timer\": 1000, "
  3246. "\"subnet6\": [ ], "
  3247. "\"valid-lifetime\": 4000 }")));
  3248. // returned value should be 0 (success)
  3249. checkResult(status, 0);
  3250. // The following codes should be enabled now
  3251. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(10));
  3252. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(20));
  3253. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(30));
  3254. // This option is on the IANA list, so it should be allowed all the time
  3255. // (http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml)
  3256. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3257. ->enabled(D6O_ERP_LOCAL_DOMAIN_NAME));
  3258. // Those options are not enabled
  3259. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(25));
  3260. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(1));
  3261. }
  3262. /// The goal of this test is to verify that configuration can include
  3263. /// Relay Supplied options (specified as names).
  3264. TEST_F(Dhcp6ParserTest, rsooNames) {
  3265. ConstElementPtr status;
  3266. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3267. Element::fromJSON("{ " + genIfaceConfig() + ","
  3268. "\"relay-supplied-options\": [ \"dns-servers\", \"remote-id\" ],"
  3269. "\"preferred-lifetime\": 3000,"
  3270. "\"rebind-timer\": 2000, "
  3271. "\"renew-timer\": 1000, "
  3272. "\"subnet6\": [ ], "
  3273. "\"valid-lifetime\": 4000 }")));
  3274. // returned value should be 0 (success)
  3275. checkResult(status, 0);
  3276. for (uint16_t code = 0; code < D6O_NAME_SERVERS; ++code) {
  3277. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3278. ->enabled(code)) << " for option code " << code;
  3279. }
  3280. // The following code should be enabled now
  3281. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3282. ->enabled(D6O_NAME_SERVERS));
  3283. for (uint16_t code = D6O_NAME_SERVERS + 1; code < D6O_REMOTE_ID; ++code) {
  3284. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3285. ->enabled(code)) << " for option code " << code;
  3286. }
  3287. // Check remote-id. It should be enabled.
  3288. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3289. ->enabled(D6O_REMOTE_ID));
  3290. for (uint16_t code = D6O_REMOTE_ID + 1; code < D6O_ERP_LOCAL_DOMAIN_NAME; ++code) {
  3291. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3292. ->enabled(code)) << " for option code " << code;
  3293. }
  3294. // This option is on the IANA list, so it should be allowed all the time
  3295. // (http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml)
  3296. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3297. ->enabled(D6O_ERP_LOCAL_DOMAIN_NAME));
  3298. for (uint16_t code = D6O_ERP_LOCAL_DOMAIN_NAME + 1; code < 300; ++code) {
  3299. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3300. ->enabled(code)) << " for option code " << code;
  3301. }
  3302. }
  3303. TEST_F(Dhcp6ParserTest, rsooNegativeNumber) {
  3304. ConstElementPtr status;
  3305. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3306. Element::fromJSON("{ " + genIfaceConfig() + ","
  3307. "\"relay-supplied-options\": [ \"80\", \"-2\" ],"
  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, 1);
  3315. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3316. }
  3317. TEST_F(Dhcp6ParserTest, rsooBogusName) {
  3318. ConstElementPtr status;
  3319. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3320. Element::fromJSON("{ " + genIfaceConfig() + ","
  3321. "\"relay-supplied-options\": [ \"bogus\", \"dns-servers\" ],"
  3322. "\"preferred-lifetime\": 3000,"
  3323. "\"rebind-timer\": 2000, "
  3324. "\"renew-timer\": 1000, "
  3325. "\"subnet6\": [ ], "
  3326. "\"valid-lifetime\": 4000 }")));
  3327. // returned value should be 0 (success)
  3328. checkResult(status, 1);
  3329. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3330. }
  3331. /// Check that the decline-probation-period value can be set properly.
  3332. TEST_F(Dhcp6ParserTest, declineTimerDefault) {
  3333. ConstElementPtr status;
  3334. string config_txt = "{ " + genIfaceConfig() + ","
  3335. "\"subnet6\": [ ] "
  3336. "}";
  3337. ElementPtr config = Element::fromJSON(config_txt);
  3338. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config));
  3339. // returned value should be 0 (success)
  3340. checkResult(status, 0);
  3341. // The value of decline-probation-period must be equal to the
  3342. // default value.
  3343. EXPECT_EQ(DEFAULT_DECLINE_PROBATION_PERIOD,
  3344. CfgMgr::instance().getStagingCfg()->getDeclinePeriod());
  3345. }
  3346. /// Check that the decline-probation-period value can be set properly.
  3347. TEST_F(Dhcp6ParserTest, declineTimer) {
  3348. ConstElementPtr status;
  3349. string config = "{ " + genIfaceConfig() + "," +
  3350. "\"decline-probation-period\": 12345,"
  3351. "\"subnet6\": [ ]"
  3352. "}";
  3353. ElementPtr json = Element::fromJSON(config);
  3354. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3355. // returned value should be 0 (success)
  3356. checkResult(status, 0);
  3357. // The value of decline-probation-period must be equal to the
  3358. // value specified.
  3359. EXPECT_EQ(12345,
  3360. CfgMgr::instance().getStagingCfg()->getDeclinePeriod());
  3361. }
  3362. /// Check that an incorrect decline-probation-period value will be caught.
  3363. TEST_F(Dhcp6ParserTest, declineTimerError) {
  3364. ConstElementPtr status;
  3365. string config = "{ " + genIfaceConfig() + "," +
  3366. "\"decline-probation-period\": \"soon\","
  3367. "\"subnet6\": [ ]"
  3368. "}";
  3369. ElementPtr json = Element::fromJSON(config);
  3370. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3371. // returned value should be 1 (error)
  3372. checkResult(status, 1);
  3373. // Check that the error contains error position.
  3374. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3375. }
  3376. // Check that configuration for the expired leases processing may be
  3377. // specified.
  3378. TEST_F(Dhcp6ParserTest, expiredLeasesProcessing) {
  3379. // Create basic configuration with the expiration specific parameters.
  3380. string config = "{ " + genIfaceConfig() + "," +
  3381. "\"expired-leases-processing\": "
  3382. "{"
  3383. " \"reclaim-timer-wait-time\": 20,"
  3384. " \"flush-reclaimed-timer-wait-time\": 35,"
  3385. " \"hold-reclaimed-time\": 1800,"
  3386. " \"max-reclaim-leases\": 50,"
  3387. " \"max-reclaim-time\": 100,"
  3388. " \"unwarned-reclaim-cycles\": 10"
  3389. "},"
  3390. "\"subnet6\": [ ]"
  3391. "}";
  3392. ElementPtr json = Element::fromJSON(config);
  3393. ConstElementPtr status;
  3394. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3395. // Returned value should be 0 (success)
  3396. checkResult(status, 0);
  3397. // The value of decline-probation-period must be equal to the
  3398. // value specified.
  3399. CfgExpirationPtr cfg = CfgMgr::instance().getStagingCfg()->getCfgExpiration();
  3400. ASSERT_TRUE(cfg);
  3401. // Verify that parameters are correct.
  3402. EXPECT_EQ(20, cfg->getReclaimTimerWaitTime());
  3403. EXPECT_EQ(35, cfg->getFlushReclaimedTimerWaitTime());
  3404. EXPECT_EQ(1800, cfg->getHoldReclaimedTime());
  3405. EXPECT_EQ(50, cfg->getMaxReclaimLeases());
  3406. EXPECT_EQ(100, cfg->getMaxReclaimTime());
  3407. EXPECT_EQ(10, cfg->getUnwarnedReclaimCycles());
  3408. }
  3409. // Check that invalid configuration for the expired leases processing is
  3410. // causing an error.
  3411. TEST_F(Dhcp6ParserTest, expiredLeasesProcessingError) {
  3412. // Create basic configuration with the expiration specific parameters.
  3413. // One of the parameters holds invalid value.
  3414. string config = "{ " + genIfaceConfig() + "," +
  3415. "\"expired-leases-processing\": "
  3416. "{"
  3417. " \"reclaim-timer-wait-time\": -5,"
  3418. " \"flush-reclaimed-timer-wait-time\": 35,"
  3419. " \"hold-reclaimed-time\": 1800,"
  3420. " \"max-reclaim-leases\": 50,"
  3421. " \"max-reclaim-time\": 100,"
  3422. " \"unwarned-reclaim-cycles\": 10"
  3423. "},"
  3424. "\"subnet6\": [ ]"
  3425. "}";
  3426. ElementPtr json = Element::fromJSON(config);
  3427. ConstElementPtr status;
  3428. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3429. // Returned value should be 0 (error)
  3430. checkResult(status, 1);
  3431. // Check that the error contains error position.
  3432. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3433. }
  3434. // Verifies that simple list of valid classes parses and
  3435. // is staged for commit.
  3436. TEST_F(Dhcp6ParserTest, validClientClassDictionary) {
  3437. string config = "{ " + genIfaceConfig() + ","
  3438. "\"preferred-lifetime\": 3000, \n"
  3439. "\"rebind-timer\": 2000, \n"
  3440. "\"renew-timer\": 1000, \n"
  3441. "\"client-classes\" : [ \n"
  3442. " { \n"
  3443. " \"name\": \"one\" \n"
  3444. " }, \n"
  3445. " { \n"
  3446. " \"name\": \"two\" \n"
  3447. " }, \n"
  3448. " { \n"
  3449. " \"name\": \"three\" \n"
  3450. " } \n"
  3451. "], \n"
  3452. "\"subnet6\": [ { \n"
  3453. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ], \n"
  3454. " \"subnet\": \"2001:db8:1::/64\" } ], \n"
  3455. "\"valid-lifetime\": 4000 } \n";
  3456. ConstElementPtr status;
  3457. ElementPtr json = Element::fromJSON(config);
  3458. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3459. ASSERT_TRUE(status);
  3460. checkResult(status, 0);
  3461. // We check staging config because CfgMgr::commit hasn't been executed.
  3462. ClientClassDictionaryPtr dictionary;
  3463. dictionary = CfgMgr::instance().getStagingCfg()->getClientClassDictionary();
  3464. ASSERT_TRUE(dictionary);
  3465. EXPECT_EQ(3, dictionary->getClasses()->size());
  3466. // Execute the commit
  3467. ASSERT_NO_THROW(CfgMgr::instance().commit());
  3468. // Verify that after commit, the current config has the correct dictionary
  3469. dictionary = CfgMgr::instance().getCurrentCfg()->getClientClassDictionary();
  3470. ASSERT_TRUE(dictionary);
  3471. EXPECT_EQ(3, dictionary->getClasses()->size());
  3472. }
  3473. // Verifies that a class list containing an invalid
  3474. // class definition causes a configuraiton error.
  3475. TEST_F(Dhcp6ParserTest, invalidClientClassDictionary) {
  3476. string config = "{ " + genIfaceConfig() + "," +
  3477. "\"valid-lifetime\": 4000, \n"
  3478. "\"rebind-timer\": 2000, \n"
  3479. "\"renew-timer\": 1000, \n"
  3480. "\"client-classes\" : [ \n"
  3481. " { \n"
  3482. " \"name\": \"one\", \n"
  3483. " \"bogus\": \"bad\" \n"
  3484. " } \n"
  3485. "], \n"
  3486. "\"subnet4\": [ { \n"
  3487. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], \n"
  3488. " \"subnet\": \"192.0.2.0/24\" \n"
  3489. " } ] \n"
  3490. "} \n";
  3491. ConstElementPtr status;
  3492. ElementPtr json = Element::fromJSON(config);
  3493. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3494. ASSERT_TRUE(status);
  3495. checkResult(status, 1);
  3496. }
  3497. };