config_parser_unittest.cc 146 KB

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