config_parser_unittest.cc 123 KB

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