config_parser_unittest.cc 143 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899
  1. // Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <config/ccsession.h>
  16. #include <dhcp/libdhcp++.h>
  17. #include <dhcp/option6_ia.h>
  18. #include <dhcp/iface_mgr.h>
  19. #include <dhcp/option_custom.h>
  20. #include <dhcp/option_int.h>
  21. #include <dhcp/tests/iface_mgr_test_config.h>
  22. #include <dhcp6/json_config_parser.h>
  23. #include <dhcp6/dhcp6_srv.h>
  24. #include <dhcpsrv/addr_utilities.h>
  25. #include <dhcpsrv/cfgmgr.h>
  26. #include <dhcpsrv/cfg_hosts.h>
  27. #include <dhcpsrv/subnet.h>
  28. #include <dhcpsrv/subnet_selector.h>
  29. #include <dhcpsrv/testutils/config_result_check.h>
  30. #include <hooks/hooks_manager.h>
  31. #include "test_data_files_config.h"
  32. #include "test_libraries.h"
  33. #include "marker_file.h"
  34. #include <boost/foreach.hpp>
  35. #include <gtest/gtest.h>
  36. #include <fstream>
  37. #include <iostream>
  38. #include <fstream>
  39. #include <sstream>
  40. #include <string>
  41. #include <vector>
  42. #include <arpa/inet.h>
  43. #include <unistd.h>
  44. using namespace isc;
  45. using namespace isc::asiolink;
  46. using namespace isc::config;
  47. using namespace isc::data;
  48. using namespace isc::dhcp;
  49. using namespace isc::dhcp::test;
  50. using namespace isc::hooks;
  51. using namespace std;
  52. namespace {
  53. std::string specfile(const std::string& name) {
  54. return (std::string(DHCP6_SRC_DIR) + "/" + name);
  55. }
  56. /// @brief Tests that the spec file is valid.
  57. /// Verifies that the DHCP6 configuration specification file is valid.
  58. TEST(Dhcp6SpecTest, basicSpec) {
  59. ASSERT_NO_THROW(isc::config::
  60. moduleSpecFromFile(specfile("dhcp6.spec")));
  61. }
  62. class Dhcp6ParserTest : public ::testing::Test {
  63. protected:
  64. // Check that no hooks libraries are loaded. This is a pre-condition for
  65. // a number of tests, so is checked in one place. As this uses an
  66. // ASSERT call - and it is not clear from the documentation that Gtest
  67. // predicates can be used in a constructor - the check is placed in SetUp.
  68. virtual void SetUp() {
  69. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  70. ASSERT_TRUE(libraries.empty());
  71. }
  72. public:
  73. Dhcp6ParserTest() :rcode_(-1), srv_(0) {
  74. // srv_(0) means to not open any sockets. We don't want to
  75. // deal with sockets here, just check if configuration handling
  76. // is sane.
  77. const IfaceMgr::IfaceCollection& ifaces =
  78. IfaceMgr::instance().getIfaces();
  79. // There must be some interface detected
  80. if (ifaces.empty()) {
  81. // We can't use ASSERT in constructor
  82. ADD_FAILURE() << "No interfaces detected.";
  83. }
  84. valid_iface_ = (*ifaces.begin())->getName();
  85. bogus_iface_ = "nonexisting0";
  86. if (IfaceMgr::instance().getIface(bogus_iface_)) {
  87. ADD_FAILURE() << "The '" << bogus_iface_ << "' exists on this system"
  88. << " while the test assumes that it doesn't, to execute"
  89. << " some negative scenarios. Can't continue this test.";
  90. }
  91. // Reset configuration for each test.
  92. resetConfiguration();
  93. }
  94. ~Dhcp6ParserTest() {
  95. // Reset configuration database after each test.
  96. resetConfiguration();
  97. // ... and delete the hooks library marker files if present
  98. unlink(LOAD_MARKER_FILE);
  99. unlink(UNLOAD_MARKER_FILE);
  100. };
  101. // Checks if config_result (result of DHCP server configuration) has
  102. // expected code (0 for success, other for failures).
  103. // Also stores result in rcode_ and comment_.
  104. void checkResult(ConstElementPtr status, int expected_code) {
  105. ASSERT_TRUE(status);
  106. comment_ = parseAnswer(rcode_, status);
  107. EXPECT_EQ(expected_code, rcode_);
  108. }
  109. /// @brief Returns an interface configuration used by the most of the
  110. /// unit tests.
  111. std::string genIfaceConfig() const {
  112. return ("\"interfaces-config\": {"
  113. " \"interfaces\": [ \"*\" ]"
  114. "}");
  115. }
  116. /// @brief Create the simple configuration with single option.
  117. ///
  118. /// This function allows to set one of the parameters that configure
  119. /// option value. These parameters are: "name", "code", "data" and
  120. /// "csv-format".
  121. ///
  122. /// @param param_value string holding option parameter value to be
  123. /// injected into the configuration string.
  124. /// @param parameter name of the parameter to be configured with
  125. /// param value.
  126. std::string createConfigWithOption(const std::string& param_value,
  127. const std::string& parameter) {
  128. std::map<std::string, std::string> params;
  129. if (parameter == "name") {
  130. params["name"] = param_value;
  131. params["space"] = "dhcp6";
  132. params["code"] = "38";
  133. params["data"] = "ABCDEF0105";
  134. params["csv-format"] = "False";
  135. } else if (parameter == "space") {
  136. params["name"] = "subscriber-id";
  137. params["space"] = param_value;
  138. params["code"] = "38";
  139. params["data"] = "ABCDEF0105";
  140. params["csv-format"] = "False";
  141. } else if (parameter == "code") {
  142. params["name"] = "subscriber-id";
  143. params["space"] = "dhcp6";
  144. params["code"] = param_value;
  145. params["data"] = "ABCDEF0105";
  146. params["csv-format"] = "False";
  147. } else if (parameter == "data") {
  148. params["name"] = "subscriber-id";
  149. params["space"] = "dhcp6";
  150. params["code"] = "38";
  151. params["data"] = param_value;
  152. params["csv-format"] = "False";
  153. } else if (parameter == "csv-format") {
  154. params["name"] = "subscriber-id";
  155. params["space"] = "dhcp6";
  156. params["code"] = "38";
  157. params["data"] = "ABCDEF0105";
  158. params["csv-format"] = param_value;
  159. }
  160. return (createConfigWithOption(params));
  161. }
  162. /// @brief Create simple configuration with single option.
  163. ///
  164. /// This function creates a configuration for a single option with
  165. /// custom values for all parameters that describe the option.
  166. ///
  167. /// @params params map holding parameters and their values.
  168. /// @return configuration string containing custom values of parameters
  169. /// describing an option.
  170. std::string createConfigWithOption(const std::map<std::string,
  171. std::string>& params)
  172. {
  173. std::ostringstream stream;
  174. stream << "{ " << genIfaceConfig() << ","
  175. "\"preferred-lifetime\": 3000,"
  176. "\"rebind-timer\": 2000, "
  177. "\"renew-timer\": 1000, "
  178. "\"option-def\": [ {"
  179. " \"name\": \"bool-option\","
  180. " \"code\": 1000,"
  181. " \"type\": \"boolean\","
  182. " \"array\": False,"
  183. " \"record-types\": \"\","
  184. " \"space\": \"dhcp6\","
  185. " \"encapsulate\": \"\""
  186. "} ],"
  187. "\"subnet6\": [ { "
  188. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  189. " \"subnet\": \"2001:db8:1::/64\", "
  190. " \"option-data\": [ {";
  191. bool first = true;
  192. typedef std::pair<std::string, std::string> ParamPair;
  193. BOOST_FOREACH(ParamPair param, params) {
  194. if (!first) {
  195. stream << ", ";
  196. } else {
  197. // cppcheck-suppress unreadVariable
  198. first = false;
  199. }
  200. if (param.first == "name") {
  201. stream << "\"name\": \"" << param.second << "\"";
  202. } else if (param.first == "space") {
  203. stream << "\"space\": \"" << param.second << "\"";
  204. } else if (param.first == "code") {
  205. stream << "\"code\": " << param.second;;
  206. } else if (param.first == "data") {
  207. stream << "\"data\": \"" << param.second << "\"";
  208. } else if (param.first == "csv-format") {
  209. stream << "\"csv-format\": " << param.second;
  210. }
  211. }
  212. stream <<
  213. " } ]"
  214. " } ],"
  215. "\"valid-lifetime\": 4000 }";
  216. return (stream.str());
  217. }
  218. /// @brief Returns an option from the subnet.
  219. ///
  220. /// This function returns an option from a subnet to which the
  221. /// specified subnet address belongs. The option is identified
  222. /// by its code.
  223. ///
  224. /// @param subnet_address Address which belongs to the subnet from
  225. /// which the option is to be returned.
  226. /// @param option_code Code of the option to be returned.
  227. /// @param expected_options_count Expected number of options in
  228. /// the particular subnet.
  229. ///
  230. /// @return Descriptor of the option. If the descriptor holds a
  231. /// NULL option pointer, it means that there was no such option
  232. /// in the subnet.
  233. OptionDescriptor
  234. getOptionFromSubnet(const IOAddress& subnet_address,
  235. const uint16_t option_code,
  236. const uint16_t expected_options_count = 1) {
  237. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  238. selectSubnet(subnet_address, classify_);
  239. if (!subnet) {
  240. /// @todo replace toText() with the use of operator <<.
  241. ADD_FAILURE() << "A subnet for the specified address "
  242. << subnet_address.toText()
  243. << " does not exist in Config Manager";
  244. }
  245. OptionContainerPtr options =
  246. subnet->getCfgOption()->getAll("dhcp6");
  247. if (expected_options_count != options->size()) {
  248. ADD_FAILURE() << "The number of options in the subnet '"
  249. << subnet_address.toText() << "' is different "
  250. " than expected number of options '"
  251. << expected_options_count << "'";
  252. }
  253. // Get the search index. Index #1 is to search using option code.
  254. const OptionContainerTypeIndex& idx = options->get<1>();
  255. // Get the options for specified index. Expecting one option to be
  256. // returned but in theory we may have multiple options with the same
  257. // code so we get the range.
  258. std::pair<OptionContainerTypeIndex::const_iterator,
  259. OptionContainerTypeIndex::const_iterator> range =
  260. idx.equal_range(option_code);
  261. if (std::distance(range.first, range.second) > 1) {
  262. ADD_FAILURE() << "There is more than one option having the"
  263. " option code '" << option_code << "' in a subnet '"
  264. << subnet_address.toText() << "'. Expected "
  265. " at most one option";
  266. } else if (std::distance(range.first, range.second) == 0) {
  267. return (OptionDescriptor(OptionPtr(), false));
  268. }
  269. return (*range.first);
  270. }
  271. /// @brief Parse and Execute configuration
  272. ///
  273. /// Parses a configuration and executes a configuration of the server.
  274. /// If the operation fails, the current test will register a failure.
  275. ///
  276. /// @param config Configuration to parse
  277. /// @param operation Operation being performed. In the case of an error,
  278. /// the error text will include the string "unable to <operation>.".
  279. ///
  280. /// @return true if the configuration succeeded, false if not. In the
  281. /// latter case, a failure will have been added to the current test.
  282. bool
  283. executeConfiguration(const std::string& config, const char* operation) {
  284. ConstElementPtr status;
  285. try {
  286. ElementPtr json = Element::fromJSON(config);
  287. status = configureDhcp6Server(srv_, json);
  288. } catch (const std::exception& ex) {
  289. ADD_FAILURE() << "Unable to " << operation << ". "
  290. << "The following configuration was used: " << std::endl
  291. << config << std::endl
  292. << " and the following error message was returned:"
  293. << ex.what() << std::endl;
  294. return (false);
  295. }
  296. // The status object must not be NULL
  297. if (!status) {
  298. ADD_FAILURE() << "Unable to " << operation << ". "
  299. << "The configuration function returned a null pointer.";
  300. return (false);
  301. }
  302. // Store the answer if we need it.
  303. // Returned value should be 0 (configuration success)
  304. comment_ = parseAnswer(rcode_, status);
  305. if (rcode_ != 0) {
  306. string reason = "";
  307. if (comment_) {
  308. reason = string(" (") + comment_->stringValue() + string(")");
  309. }
  310. ADD_FAILURE() << "Unable to " << operation << ". "
  311. << "The configuration function returned error code "
  312. << rcode_ << reason;
  313. return (false);
  314. }
  315. return (true);
  316. }
  317. /// @brief Reset configuration database.
  318. ///
  319. /// This function resets configuration data base by removing all subnets
  320. /// option-data, and hooks libraries. The reset must be performed after each
  321. /// test to make sure that contents of the database do not affect the
  322. /// results of subsequent tests.
  323. void resetConfiguration() {
  324. string config = "{ \"interfaces-config\": {"
  325. " \"interfaces\": [ ]"
  326. "},"
  327. "\"hooks-libraries\": [ ],"
  328. "\"preferred-lifetime\": 3000,"
  329. "\"rebind-timer\": 2000, "
  330. "\"renew-timer\": 1000, "
  331. "\"valid-lifetime\": 4000, "
  332. "\"subnet6\": [ ], "
  333. "\"dhcp-ddns\": { \"enable-updates\" : false }, "
  334. "\"option-def\": [ ], "
  335. "\"option-data\": [ ] }";
  336. static_cast<void>(executeConfiguration(config,
  337. "reset configuration database"));
  338. // The default setting is to listen on all interfaces. In order to
  339. // properly test interface configuration we disable listening on
  340. // all interfaces before each test and later check that this setting
  341. // has been overriden by the configuration used in the test.
  342. CfgMgr::instance().clear();
  343. // Create fresh context.
  344. globalContext()->copyContext(ParserContext(Option::V6));
  345. }
  346. /// @brief Test invalid option parameter value.
  347. ///
  348. /// This test function constructs the simple configuration
  349. /// string and injects invalid option configuration into it.
  350. /// It expects that parser will fail with provided option code.
  351. ///
  352. /// @param param_value string holding invalid option parameter value
  353. /// to be injected into configuration string.
  354. /// @param parameter name of the parameter to be configured with
  355. /// param_value (can be any of "name", "code", "data")
  356. void testInvalidOptionParam(const std::string& param_value,
  357. const std::string& parameter) {
  358. ConstElementPtr x;
  359. std::string config = createConfigWithOption(param_value, parameter);
  360. ElementPtr json = Element::fromJSON(config);
  361. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  362. checkResult(x, 1);
  363. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  364. CfgMgr::instance().clear();
  365. }
  366. /// @brief Test invalid option paramater value.
  367. ///
  368. /// This test function constructs the simple configuration
  369. /// string and injects invalid option configuration into it.
  370. /// It expects that parser will fail with provided option code.
  371. ///
  372. /// @param params Map of parameters defining an option.
  373. void
  374. testInvalidOptionParam(const std::map<std::string, std::string>& params) {
  375. ConstElementPtr x;
  376. std::string config = createConfigWithOption(params);
  377. ElementPtr json = Element::fromJSON(config);
  378. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  379. checkResult(x, 1);
  380. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  381. CfgMgr::instance().clear();
  382. }
  383. /// @brief Test option against given code and data.
  384. ///
  385. /// @param option_desc option descriptor that carries the option to
  386. /// be tested.
  387. /// @param expected_code expected code of the option.
  388. /// @param expected_data expected data in the option.
  389. /// @param expected_data_len length of the reference data.
  390. /// @param extra_data if true extra data is allowed in an option
  391. /// after tested data.
  392. void testOption(const OptionDescriptor& option_desc,
  393. uint16_t expected_code, const uint8_t* expected_data,
  394. size_t expected_data_len,
  395. bool extra_data = false) {
  396. // Check if option descriptor contains valid option pointer.
  397. ASSERT_TRUE(option_desc.option_);
  398. // Verify option type.
  399. EXPECT_EQ(expected_code, option_desc.option_->getType());
  400. // We may have many different option types being created. Some of them
  401. // have dedicated classes derived from Option class. In such case if
  402. // we want to verify the option contents against expected_data we have
  403. // to prepare raw buffer with the contents of the option. The easiest
  404. // way is to call pack() which will prepare on-wire data.
  405. util::OutputBuffer buf(option_desc.option_->getData().size());
  406. option_desc.option_->pack(buf);
  407. if (extra_data) {
  408. // The length of the buffer must be at least equal to size of the
  409. // reference data but it can sometimes be greater than that. This is
  410. // because some options carry suboptions that increase the overall
  411. // length.
  412. ASSERT_GE(buf.getLength() - option_desc.option_->getHeaderLen(),
  413. expected_data_len);
  414. } else {
  415. ASSERT_EQ(buf.getLength() - option_desc.option_->getHeaderLen(),
  416. expected_data_len);
  417. }
  418. // Verify that the data is correct. Do not verify suboptions and a header.
  419. const uint8_t* data = static_cast<const uint8_t*>(buf.getData());
  420. EXPECT_EQ(0, memcmp(expected_data, data + option_desc.option_->getHeaderLen(),
  421. expected_data_len));
  422. }
  423. /// @brief Test option configuration.
  424. ///
  425. /// This function creates a configuration for a specified option using
  426. /// a map of parameters specified as the argument. The map holds
  427. /// name/value pairs which identifies option's configuration parameters:
  428. /// - name
  429. /// - space
  430. /// - code
  431. /// - data
  432. /// - csv-format.
  433. /// This function applies a new server configuration and checks that the
  434. /// option being configured is inserted into CfgMgr. The raw contents of
  435. /// this option are compared with the binary data specified as expected
  436. /// data passed to this function.
  437. ///
  438. /// @param params Map of parameters defining an option.
  439. /// @param option_code Option code.
  440. /// @param expected_data Array containing binary data expected to be stored
  441. /// in the configured option.
  442. /// @param expected_data_len Length of the array holding reference data.
  443. void testConfiguration(const std::map<std::string, std::string>& params,
  444. const uint16_t option_code,
  445. const uint8_t* expected_data,
  446. const size_t expected_data_len) {
  447. CfgMgr::instance().clear();
  448. std::string config = createConfigWithOption(params);
  449. ASSERT_TRUE(executeConfiguration(config, "parse option configuration"));
  450. // The subnet should now hold one option with the specified code.
  451. OptionDescriptor desc =
  452. getOptionFromSubnet(IOAddress("2001:db8:1::5"), option_code);
  453. ASSERT_TRUE(desc.option_);
  454. testOption(desc, option_code, expected_data, expected_data_len);
  455. CfgMgr::instance().clear();
  456. }
  457. int rcode_; ///< Return code (see @ref isc::config::parseAnswer)
  458. Dhcpv6Srv srv_; ///< Instance of the Dhcp6Srv used during tests
  459. ConstElementPtr comment_; ///< Comment (see @ref isc::config::parseAnswer)
  460. string valid_iface_; ///< Valid network interface name (present in system)
  461. string bogus_iface_; ///< invalid network interface name (not in system)
  462. isc::dhcp::ClientClasses classify_; ///< used in client classification
  463. };
  464. // Goal of this test is a verification if a very simple config update
  465. // with just a bumped version number. That's the simplest possible
  466. // config update.
  467. TEST_F(Dhcp6ParserTest, version) {
  468. ConstElementPtr x;
  469. EXPECT_NO_THROW(x = configureDhcp6Server(srv_,
  470. Element::fromJSON("{\"version\": 0}")));
  471. // returned value must be 0 (configuration accepted)
  472. checkResult(x, 0);
  473. }
  474. /// The goal of this test is to verify that the code accepts only
  475. /// valid commands and malformed or unsupported parameters are rejected.
  476. TEST_F(Dhcp6ParserTest, bogusCommand) {
  477. ConstElementPtr x;
  478. EXPECT_NO_THROW(x = configureDhcp6Server(srv_,
  479. Element::fromJSON("{\"bogus\": 5}")));
  480. // returned value must be 1 (configuration parse error)
  481. checkResult(x, 1);
  482. }
  483. /// The goal of this test is to verify if configuration without any
  484. /// subnets defined can be accepted.
  485. TEST_F(Dhcp6ParserTest, emptySubnet) {
  486. ConstElementPtr status;
  487. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  488. Element::fromJSON("{ " + genIfaceConfig() + ","
  489. "\"preferred-lifetime\": 3000,"
  490. "\"rebind-timer\": 2000, "
  491. "\"renew-timer\": 1000, "
  492. "\"subnet6\": [ ], "
  493. "\"valid-lifetime\": 4000 }")));
  494. // returned value should be 0 (success)
  495. checkResult(status, 0);
  496. }
  497. /// The goal of this test is to verify if defined subnet uses global
  498. /// parameter timer definitions.
  499. TEST_F(Dhcp6ParserTest, subnetGlobalDefaults) {
  500. ConstElementPtr status;
  501. string config = "{ " + genIfaceConfig() + ","
  502. "\"preferred-lifetime\": 3000,"
  503. "\"rebind-timer\": 2000, "
  504. "\"renew-timer\": 1000, "
  505. "\"subnet6\": [ { "
  506. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  507. " \"subnet\": \"2001:db8:1::/64\" } ],"
  508. "\"valid-lifetime\": 4000 }";
  509. ElementPtr json = Element::fromJSON(config);
  510. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  511. // check if returned status is OK
  512. checkResult(status, 0);
  513. // Now check if the configuration was indeed handled and we have
  514. // expected pool configured.
  515. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  516. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  517. ASSERT_TRUE(subnet);
  518. EXPECT_EQ(1000, subnet->getT1());
  519. EXPECT_EQ(2000, subnet->getT2());
  520. EXPECT_EQ(3000, subnet->getPreferred());
  521. EXPECT_EQ(4000, subnet->getValid());
  522. // Check that subnet-id is 1
  523. EXPECT_EQ(1, subnet->getID());
  524. }
  525. // This test checks that multiple subnets can be defined and handled properly.
  526. TEST_F(Dhcp6ParserTest, multipleSubnets) {
  527. ConstElementPtr x;
  528. // Collection of four subnets for which ids should be autogenerated
  529. // - ids are unspecified or set to 0.
  530. string config = "{ " + genIfaceConfig() + ","
  531. "\"preferred-lifetime\": 3000,"
  532. "\"rebind-timer\": 2000, "
  533. "\"renew-timer\": 1000, "
  534. "\"subnet6\": [ { "
  535. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  536. " \"subnet\": \"2001:db8:1::/64\" "
  537. " },"
  538. " {"
  539. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  540. " \"subnet\": \"2001:db8:2::/64\", "
  541. " \"id\": 0"
  542. " },"
  543. " {"
  544. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  545. " \"subnet\": \"2001:db8:3::/64\" "
  546. " },"
  547. " {"
  548. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  549. " \"subnet\": \"2001:db8:4::/64\" "
  550. " } ],"
  551. "\"valid-lifetime\": 4000 }";
  552. int cnt = 0; // Number of reconfigurations
  553. ElementPtr json = Element::fromJSON(config);
  554. do {
  555. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  556. checkResult(x, 0);
  557. CfgMgr::instance().commit();
  558. const Subnet6Collection* subnets =
  559. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  560. ASSERT_TRUE(subnets);
  561. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  562. // Check subnet-ids of each subnet (it should be monotonously increasing)
  563. EXPECT_EQ(1, subnets->at(0)->getID());
  564. EXPECT_EQ(2, subnets->at(1)->getID());
  565. EXPECT_EQ(3, subnets->at(2)->getID());
  566. EXPECT_EQ(4, subnets->at(3)->getID());
  567. // Repeat reconfiguration process 10 times and check that the subnet-id
  568. // is set to the same value. Technically, just two iterations would be
  569. // sufficient, but it's nice to have a test that exercises reconfiguration
  570. // a bit.
  571. } while (++cnt < 10);
  572. }
  573. // This checks that it is possible to assign arbitrary ids for subnets.
  574. TEST_F(Dhcp6ParserTest, multipleSubnetsExplicitIDs) {
  575. ConstElementPtr x;
  576. // Four subnets with arbitrary subnet ids.
  577. string config = "{ " + genIfaceConfig() + ","
  578. "\"preferred-lifetime\": 3000,"
  579. "\"rebind-timer\": 2000, "
  580. "\"renew-timer\": 1000, "
  581. "\"subnet6\": [ { "
  582. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  583. " \"subnet\": \"2001:db8:1::/64\", "
  584. " \"id\": 1024"
  585. " },"
  586. " {"
  587. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  588. " \"subnet\": \"2001:db8:2::/64\", "
  589. " \"id\": 100"
  590. " },"
  591. " {"
  592. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  593. " \"subnet\": \"2001:db8:3::/64\", "
  594. " \"id\": 1"
  595. " },"
  596. " {"
  597. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  598. " \"subnet\": \"2001:db8:4::/64\", "
  599. " \"id\": 34"
  600. " } ],"
  601. "\"valid-lifetime\": 4000 }";
  602. int cnt = 0; // Number of reconfigurations
  603. ElementPtr json = Element::fromJSON(config);
  604. do {
  605. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  606. checkResult(x, 0);
  607. CfgMgr::instance().commit();
  608. const Subnet6Collection* subnets =
  609. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  610. ASSERT_TRUE(subnets);
  611. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  612. // Check that subnet ids are as expected.
  613. EXPECT_EQ(1024, subnets->at(0)->getID());
  614. EXPECT_EQ(100, subnets->at(1)->getID());
  615. EXPECT_EQ(1, subnets->at(2)->getID());
  616. EXPECT_EQ(34, subnets->at(3)->getID());
  617. // Repeat reconfiguration process 10 times and check that the subnet-id
  618. // is set to the same value.
  619. } while (++cnt < 3);
  620. }
  621. // CHeck that the configuration with two subnets having the same id is rejected.
  622. TEST_F(Dhcp6ParserTest, multipleSubnetsOverlapingIDs) {
  623. ConstElementPtr x;
  624. // Four subnets, two of them have the same id.
  625. string config = "{ " + genIfaceConfig() + ","
  626. "\"preferred-lifetime\": 3000,"
  627. "\"rebind-timer\": 2000, "
  628. "\"renew-timer\": 1000, "
  629. "\"subnet6\": [ { "
  630. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  631. " \"subnet\": \"2001:db8:1::/64\", "
  632. " \"id\": 1024"
  633. " },"
  634. " {"
  635. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  636. " \"subnet\": \"2001:db8:2::/64\", "
  637. " \"id\": 100"
  638. " },"
  639. " {"
  640. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  641. " \"subnet\": \"2001:db8:3::/64\", "
  642. " \"id\": 1024"
  643. " },"
  644. " {"
  645. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  646. " \"subnet\": \"2001:db8:4::/64\", "
  647. " \"id\": 34"
  648. " } ],"
  649. "\"valid-lifetime\": 4000 }";
  650. ElementPtr json = Element::fromJSON(config);
  651. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  652. checkResult(x, 1);
  653. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  654. }
  655. // Goal of this test is to verify that a previously configured subnet can be
  656. // deleted in subsequent reconfiguration.
  657. TEST_F(Dhcp6ParserTest, reconfigureRemoveSubnet) {
  658. ConstElementPtr x;
  659. // All four subnets
  660. string config4 = "{ " + genIfaceConfig() + ","
  661. "\"preferred-lifetime\": 3000,"
  662. "\"rebind-timer\": 2000, "
  663. "\"renew-timer\": 1000, "
  664. "\"subnet6\": [ { "
  665. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  666. " \"subnet\": \"2001:db8:1::/64\", "
  667. " \"id\": 1"
  668. " },"
  669. " {"
  670. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  671. " \"subnet\": \"2001:db8:2::/64\", "
  672. " \"id\": 2"
  673. " },"
  674. " {"
  675. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  676. " \"subnet\": \"2001:db8:3::/64\", "
  677. " \"id\": 3"
  678. " },"
  679. " {"
  680. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  681. " \"subnet\": \"2001:db8:4::/64\", "
  682. " \"id\": 4"
  683. " } ],"
  684. "\"valid-lifetime\": 4000 }";
  685. // Three subnets (the last one removed)
  686. string config_first3 = "{ " + genIfaceConfig() + ","
  687. "\"preferred-lifetime\": 3000,"
  688. "\"rebind-timer\": 2000, "
  689. "\"renew-timer\": 1000, "
  690. "\"subnet6\": [ { "
  691. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  692. " \"subnet\": \"2001:db8:1::/64\", "
  693. " \"id\": 1"
  694. " },"
  695. " {"
  696. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  697. " \"subnet\": \"2001:db8:2::/64\", "
  698. " \"id\": 2"
  699. " },"
  700. " {"
  701. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  702. " \"subnet\": \"2001:db8:3::/64\", "
  703. " \"id\": 3"
  704. " } ],"
  705. "\"valid-lifetime\": 4000 }";
  706. // Second subnet removed
  707. string config_second_removed = "{ " + genIfaceConfig() + ","
  708. "\"preferred-lifetime\": 3000,"
  709. "\"rebind-timer\": 2000, "
  710. "\"renew-timer\": 1000, "
  711. "\"subnet6\": [ { "
  712. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  713. " \"subnet\": \"2001:db8:1::/64\", "
  714. " \"id\": 1"
  715. " },"
  716. " {"
  717. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  718. " \"subnet\": \"2001:db8:3::/64\", "
  719. " \"id\": 3"
  720. " },"
  721. " {"
  722. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  723. " \"subnet\": \"2001:db8:4::/64\", "
  724. " \"id\": 4"
  725. " } ],"
  726. "\"valid-lifetime\": 4000 }";
  727. // CASE 1: Configure 4 subnets, then reconfigure and remove the
  728. // last one.
  729. ElementPtr json = Element::fromJSON(config4);
  730. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  731. checkResult(x, 0);
  732. CfgMgr::instance().commit();
  733. const Subnet6Collection* subnets =
  734. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  735. ASSERT_TRUE(subnets);
  736. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  737. // Do the reconfiguration (the last subnet is removed)
  738. json = Element::fromJSON(config_first3);
  739. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  740. checkResult(x, 0);
  741. CfgMgr::instance().commit();
  742. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  743. ASSERT_TRUE(subnets);
  744. ASSERT_EQ(3, subnets->size()); // We expect 3 subnets now (4th is removed)
  745. EXPECT_EQ(1, subnets->at(0)->getID());
  746. EXPECT_EQ(2, subnets->at(1)->getID());
  747. EXPECT_EQ(3, subnets->at(2)->getID());
  748. /// CASE 2: Configure 4 subnets, then reconfigure and remove one
  749. /// from in between (not first, not last)
  750. json = Element::fromJSON(config4);
  751. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  752. checkResult(x, 0);
  753. CfgMgr::instance().commit();
  754. // Do reconfiguration
  755. json = Element::fromJSON(config_second_removed);
  756. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  757. checkResult(x, 0);
  758. CfgMgr::instance().commit();
  759. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  760. ASSERT_TRUE(subnets);
  761. ASSERT_EQ(3, subnets->size()); // We expect 4 subnets
  762. EXPECT_EQ(1, subnets->at(0)->getID());
  763. // The second subnet (with subnet-id = 2) is no longer there
  764. EXPECT_EQ(3, subnets->at(1)->getID());
  765. EXPECT_EQ(4, subnets->at(2)->getID());
  766. }
  767. // This test checks if it is possible to override global values
  768. // on a per subnet basis.
  769. TEST_F(Dhcp6ParserTest, subnetLocal) {
  770. ConstElementPtr status;
  771. string config = "{ " + genIfaceConfig() + ","
  772. "\"preferred-lifetime\": 3000,"
  773. "\"rebind-timer\": 2000, "
  774. "\"renew-timer\": 1000, "
  775. "\"subnet6\": [ { "
  776. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  777. " \"renew-timer\": 1, "
  778. " \"rebind-timer\": 2, "
  779. " \"preferred-lifetime\": 3,"
  780. " \"valid-lifetime\": 4,"
  781. " \"subnet\": \"2001:db8:1::/64\" } ],"
  782. "\"valid-lifetime\": 4000 }";
  783. ElementPtr json = Element::fromJSON(config);
  784. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  785. // returned value should be 0 (configuration success)
  786. checkResult(status, 0);
  787. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  788. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  789. ASSERT_TRUE(subnet);
  790. EXPECT_EQ(1, subnet->getT1());
  791. EXPECT_EQ(2, subnet->getT2());
  792. EXPECT_EQ(3, subnet->getPreferred());
  793. EXPECT_EQ(4, subnet->getValid());
  794. }
  795. // This test checks if it is possible to define a subnet with an
  796. // interface defined.
  797. TEST_F(Dhcp6ParserTest, subnetInterface) {
  798. ConstElementPtr status;
  799. // There should be at least one interface
  800. string config = "{ " + genIfaceConfig() + ","
  801. "\"preferred-lifetime\": 3000,"
  802. "\"rebind-timer\": 2000, "
  803. "\"renew-timer\": 1000, "
  804. "\"subnet6\": [ { "
  805. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  806. " \"interface\": \"" + valid_iface_ + "\","
  807. " \"subnet\": \"2001:db8:1::/64\" } ],"
  808. "\"valid-lifetime\": 4000 }";
  809. cout << config << endl;
  810. ElementPtr json = Element::fromJSON(config);
  811. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  812. // returned value should be 0 (configuration success)
  813. checkResult(status, 0);
  814. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  815. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  816. ASSERT_TRUE(subnet);
  817. EXPECT_EQ(valid_iface_, subnet->getIface());
  818. }
  819. // This test checks if invalid interface name will be rejected in
  820. // Subnet6 definition.
  821. TEST_F(Dhcp6ParserTest, subnetInterfaceBogus) {
  822. ConstElementPtr status;
  823. // There should be at least one interface
  824. string config = "{ " + genIfaceConfig() + ","
  825. "\"preferred-lifetime\": 3000,"
  826. "\"rebind-timer\": 2000, "
  827. "\"renew-timer\": 1000, "
  828. "\"subnet6\": [ { "
  829. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  830. " \"interface\": \"" + bogus_iface_ + "\","
  831. " \"subnet\": \"2001:db8:1::/64\" } ],"
  832. "\"valid-lifetime\": 4000 }";
  833. cout << config << endl;
  834. ElementPtr json = Element::fromJSON(config);
  835. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  836. // returned value should be 1 (configuration error)
  837. checkResult(status, 1);
  838. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  839. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  840. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  841. EXPECT_FALSE(subnet);
  842. }
  843. // This test checks if it is not allowed to define global interface
  844. // parameter.
  845. TEST_F(Dhcp6ParserTest, interfaceGlobal) {
  846. ConstElementPtr status;
  847. string config = "{ " + genIfaceConfig() + ","
  848. "\"preferred-lifetime\": 3000,"
  849. "\"rebind-timer\": 2000, "
  850. "\"renew-timer\": 1000, "
  851. "\"interface\": \"" + valid_iface_ + "\"," // Not valid. Can be defined in subnet only
  852. "\"subnet6\": [ { "
  853. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  854. " \"subnet\": \"2001:db8:1::/64\" } ],"
  855. "\"valid-lifetime\": 4000 }";
  856. cout << config << endl;
  857. ElementPtr json = Element::fromJSON(config);
  858. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  859. // returned value should be 1 (parse error)
  860. checkResult(status, 1);
  861. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  862. }
  863. // This test checks if it is possible to define a subnet with an
  864. // interface-id option defined.
  865. TEST_F(Dhcp6ParserTest, subnetInterfaceId) {
  866. const string valid_interface_id = "foobar";
  867. const string bogus_interface_id = "blah";
  868. // There should be at least one interface
  869. const string config = "{ "
  870. "\"preferred-lifetime\": 3000,"
  871. "\"rebind-timer\": 2000, "
  872. "\"renew-timer\": 1000, "
  873. "\"subnet6\": [ { "
  874. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  875. " \"interface-id\": \"" + valid_interface_id + "\","
  876. " \"subnet\": \"2001:db8:1::/64\" } ],"
  877. "\"valid-lifetime\": 4000 }";
  878. ElementPtr json = Element::fromJSON(config);
  879. ConstElementPtr status;
  880. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  881. // Returned value should be 0 (configuration success)
  882. checkResult(status, 0);
  883. // Try to get a subnet based on bogus interface-id option
  884. OptionBuffer tmp(bogus_interface_id.begin(), bogus_interface_id.end());
  885. SubnetSelector selector;
  886. selector.first_relay_linkaddr_ = IOAddress("5000::1");
  887. selector.interface_id_.reset(new Option(Option::V6, D6O_INTERFACE_ID, tmp));
  888. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  889. selectSubnet(selector);
  890. EXPECT_FALSE(subnet);
  891. // Now try to get subnet for valid interface-id value
  892. tmp = OptionBuffer(valid_interface_id.begin(), valid_interface_id.end());
  893. selector.interface_id_.reset(new Option(Option::V6, D6O_INTERFACE_ID, tmp));
  894. subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  895. selectSubnet(selector);
  896. ASSERT_TRUE(subnet);
  897. EXPECT_TRUE(selector.interface_id_->equals(subnet->getInterfaceId()));
  898. }
  899. // This test checks if it is not allowed to define global interface
  900. // parameter.
  901. TEST_F(Dhcp6ParserTest, interfaceIdGlobal) {
  902. const string config = "{ " + genIfaceConfig() + ","
  903. "\"preferred-lifetime\": 3000,"
  904. "\"rebind-timer\": 2000, "
  905. "\"renew-timer\": 1000, "
  906. "\"interface-id\": \"foobar\"," // Not valid. Can be defined in subnet only
  907. "\"subnet6\": [ { "
  908. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  909. " \"subnet\": \"2001:db8:1::/64\" } ],"
  910. "\"valid-lifetime\": 4000 }";
  911. ElementPtr json = Element::fromJSON(config);
  912. ConstElementPtr status;
  913. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  914. // Returned value should be 1 (parse error)
  915. checkResult(status, 1);
  916. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  917. }
  918. // This test checks if it is not possible to define a subnet with an
  919. // interface (i.e. local subnet) and interface-id (remote subnet) defined.
  920. TEST_F(Dhcp6ParserTest, subnetInterfaceAndInterfaceId) {
  921. const string config = "{ \"preferred-lifetime\": 3000,"
  922. "\"rebind-timer\": 2000, "
  923. "\"renew-timer\": 1000, "
  924. "\"subnet6\": [ { "
  925. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  926. " \"interface\": \"" + valid_iface_ + "\","
  927. " \"interface-id\": \"foobar\","
  928. " \"subnet\": \"2001:db8:1::/64\" } ],"
  929. "\"valid-lifetime\": 4000 }";
  930. ElementPtr json = Element::fromJSON(config);
  931. ConstElementPtr status;
  932. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  933. // Returned value should be 1 (configuration error)
  934. checkResult(status, 1);
  935. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  936. }
  937. // This test checks that multiple pools can be defined and handled properly.
  938. // The test defines 2 subnets, each with 2 pools.
  939. TEST_F(Dhcp6ParserTest, multiplePools) {
  940. // Collection with two subnets, each with 2 pools.
  941. string config = "{ " + genIfaceConfig() + ","
  942. "\"preferred-lifetime\": 3000,"
  943. "\"rebind-timer\": 2000, "
  944. "\"renew-timer\": 1000, "
  945. "\"subnet6\": [ { "
  946. " \"pools\": [ "
  947. " { \"pool\": \"2001:db8:1::/96\" },"
  948. " { \"pool\": \"2001:db8:1:0:abcd::/112\" }"
  949. " ],"
  950. " \"subnet\": \"2001:db8:1::/64\" "
  951. " },"
  952. " {"
  953. " \"pools\": [ "
  954. " { \"pool\": \"2001:db8:2::1 - 2001:db8:2::ff\" },"
  955. " { \"pool\": \"2001:db8:2::300 - 2001:db8:2::3ff\" }"
  956. " ],"
  957. " \"subnet\": \"2001:db8:2::/64\""
  958. " } ],"
  959. "\"valid-lifetime\": 4000 }";
  960. ElementPtr json;
  961. ASSERT_NO_THROW(json = Element::fromJSON(config));
  962. ConstElementPtr status;
  963. ASSERT_NO_THROW(status = configureDhcp6Server(srv_, json));
  964. checkResult(status, 0);
  965. const Subnet6Collection* subnets =
  966. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  967. ASSERT_TRUE(subnets);
  968. ASSERT_EQ(2, subnets->size()); // We expect 2 subnets
  969. // Check the first subnet
  970. const PoolCollection& pools1 = subnets->at(0)->getPools(Lease::TYPE_NA);
  971. ASSERT_EQ(2, pools1.size());
  972. EXPECT_EQ("type=IA_NA, 2001:db8:1::-2001:db8:1::ffff:ffff, delegated_len=128",
  973. pools1[0]->toText());
  974. EXPECT_EQ("type=IA_NA, 2001:db8:1:0:abcd::-2001:db8:1:0:abcd::ffff, delegated_len=128",
  975. pools1[1]->toText());
  976. // There shouldn't be any TA or PD pools
  977. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_TA).empty());
  978. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_PD).empty());
  979. // Check the second subnet
  980. const PoolCollection& pools2 = subnets->at(1)->getPools(Lease::TYPE_NA);
  981. ASSERT_EQ(2, pools2.size());
  982. EXPECT_EQ("type=IA_NA, 2001:db8:2::1-2001:db8:2::ff, delegated_len=128",
  983. pools2[0]->toText());
  984. EXPECT_EQ("type=IA_NA, 2001:db8:2::300-2001:db8:2::3ff, delegated_len=128",
  985. pools2[1]->toText());
  986. // There shouldn't be any TA or PD pools
  987. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_TA).empty());
  988. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_PD).empty());
  989. }
  990. // Test verifies that a subnet with pool values that do not belong to that
  991. // pool are rejected.
  992. TEST_F(Dhcp6ParserTest, poolOutOfSubnet) {
  993. ConstElementPtr status;
  994. string config = "{ " + genIfaceConfig() + ","
  995. "\"preferred-lifetime\": 3000,"
  996. "\"rebind-timer\": 2000, "
  997. "\"renew-timer\": 1000, "
  998. "\"subnet6\": [ { "
  999. " \"pools\": [ { \"pool\": \"4001:db8:1::/80\" } ],"
  1000. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1001. "\"valid-lifetime\": 4000 }";
  1002. ElementPtr json = Element::fromJSON(config);
  1003. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1004. // returned value must be 1 (values error)
  1005. // as the pool does not belong to that subnet
  1006. checkResult(status, 1);
  1007. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1008. }
  1009. // Goal of this test is to verify if pools can be defined
  1010. // using prefix/length notation. There is no separate test for min-max
  1011. // notation as it was tested in several previous tests.
  1012. // Note this test also verifies that subnets can be configured without
  1013. // prefix delegation pools.
  1014. TEST_F(Dhcp6ParserTest, poolPrefixLen) {
  1015. ConstElementPtr x;
  1016. string config = "{ " + genIfaceConfig() + ","
  1017. "\"preferred-lifetime\": 3000,"
  1018. "\"rebind-timer\": 2000, "
  1019. "\"renew-timer\": 1000, "
  1020. "\"subnet6\": [ { "
  1021. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  1022. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1023. "\"valid-lifetime\": 4000 }";
  1024. ElementPtr json = Element::fromJSON(config);
  1025. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1026. // returned value must be 1 (configuration parse error)
  1027. checkResult(x, 0);
  1028. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1029. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1030. ASSERT_TRUE(subnet);
  1031. EXPECT_EQ(1000, subnet->getT1());
  1032. EXPECT_EQ(2000, subnet->getT2());
  1033. EXPECT_EQ(3000, subnet->getPreferred());
  1034. EXPECT_EQ(4000, subnet->getValid());
  1035. }
  1036. // Goal of this test is to verify the basic parsing of a prefix delegation
  1037. // pool. It uses a single, valid pd pool.
  1038. TEST_F(Dhcp6ParserTest, pdPoolBasics) {
  1039. ConstElementPtr x;
  1040. // Define a single valid pd pool.
  1041. string config =
  1042. "{ " + genIfaceConfig() + ","
  1043. "\"preferred-lifetime\": 3000,"
  1044. "\"rebind-timer\": 2000, "
  1045. "\"renew-timer\": 1000, "
  1046. "\"subnet6\": [ { "
  1047. " \"subnet\": \"2001:db8:1::/64\","
  1048. " \"pd-pools\": ["
  1049. " { \"prefix\": \"2001:db8:1::\", "
  1050. " \"prefix-len\": 64, "
  1051. " \"delegated-len\": 128"
  1052. " } ],"
  1053. "\"valid-lifetime\": 4000 }"
  1054. "] }";
  1055. // Convert the JSON string into Elements.
  1056. ElementPtr json;
  1057. ASSERT_NO_THROW(json = Element::fromJSON(config));
  1058. // Verify that DHCP6 configuration processing succeeds.
  1059. // Returned value must be non-empty ConstElementPtr to config result.
  1060. // rcode should be 0 which indicates successful configuration processing.
  1061. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1062. checkResult(x, 0);
  1063. // Test that we can retrieve the subnet.
  1064. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1065. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1066. ASSERT_TRUE(subnet);
  1067. // Fetch the collection of PD pools. It should have 1 entry.
  1068. PoolCollection pc;
  1069. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_PD));
  1070. EXPECT_EQ(1, pc.size());
  1071. // Get a pointer to the pd pool instance, and verify its contents.
  1072. Pool6Ptr p6;
  1073. ASSERT_NO_THROW(p6 = boost::dynamic_pointer_cast<Pool6>(pc[0]));
  1074. ASSERT_TRUE(p6);
  1075. EXPECT_EQ("2001:db8:1::", p6->getFirstAddress().toText());
  1076. EXPECT_EQ(128, p6->getLength());
  1077. // prefix-len is not directly accessible after pool construction, so
  1078. // verify that it was interpreted correctly by checking the last address
  1079. // value.
  1080. isc::asiolink::IOAddress prefixAddress("2001:db8:1::");
  1081. EXPECT_EQ(lastAddrInPrefix(prefixAddress, 64), p6->getLastAddress());
  1082. }
  1083. // Goal of this test is verify that a list of PD pools can be configured.
  1084. // It also verifies that a subnet may be configured with both regular pools
  1085. // and pd pools.
  1086. TEST_F(Dhcp6ParserTest, pdPoolList) {
  1087. ConstElementPtr x;
  1088. // We will configure three pools of prefixes for the subnet. Note that
  1089. // the 3rd prefix is out of the subnet prefix (the prefix doesn't match
  1090. // the subnet prefix).
  1091. const char* prefixes[] = {
  1092. "2001:db8:1:1::",
  1093. "2001:db8:1:2::",
  1094. "3000:1:3::"
  1095. };
  1096. string config =
  1097. "{ " + genIfaceConfig() + ","
  1098. "\"preferred-lifetime\": 3000,"
  1099. "\"rebind-timer\": 2000, "
  1100. "\"renew-timer\": 1000, "
  1101. "\"subnet6\": [ { "
  1102. " \"pools\": [ { \"pool\": \"2001:db8:1:04::/80\" } ],"
  1103. " \"subnet\": \"2001:db8:1::/40\","
  1104. " \"pd-pools\": ["
  1105. " { \"prefix\": \"2001:db8:1:01::\", "
  1106. " \"prefix-len\": 72, "
  1107. " \"delegated-len\": 80"
  1108. " },"
  1109. " { \"prefix\": \"2001:db8:1:02::\", "
  1110. " \"prefix-len\": 72, "
  1111. " \"delegated-len\": 88"
  1112. " },"
  1113. " { \"prefix\": \"3000:1:03::\", "
  1114. " \"prefix-len\": 72, "
  1115. " \"delegated-len\": 96"
  1116. " }"
  1117. "],"
  1118. "\"valid-lifetime\": 4000 }"
  1119. "] }";
  1120. // Convert the JSON string into Elements.
  1121. ElementPtr json = Element::fromJSON(config);
  1122. ASSERT_NO_THROW(json = Element::fromJSON(config));
  1123. // Verify that DHCP6 configuration processing succeeds.
  1124. // Returned value must be non-empty ConstElementPtr to config result.
  1125. // rcode should be 0 which indicates successful configuration processing.
  1126. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1127. checkResult(x, 0);
  1128. // Test that we can retrieve the subnet.
  1129. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1130. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1131. ASSERT_TRUE(subnet);
  1132. // Fetch the collection of NA pools. It should have 1 entry.
  1133. PoolCollection pc;
  1134. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_NA));
  1135. EXPECT_EQ(1, pc.size());
  1136. // Fetch the collection of PD pools. It should have 3 entries.
  1137. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_PD));
  1138. EXPECT_EQ(3, pc.size());
  1139. // Loop through the pools and verify their contents.
  1140. for (int i = 0; i < 3; i++) {
  1141. Pool6Ptr p6;
  1142. ASSERT_NO_THROW(p6 = boost::dynamic_pointer_cast<Pool6>(pc[i]));
  1143. ASSERT_TRUE(p6);
  1144. EXPECT_EQ(prefixes[i], p6->getFirstAddress().toText());
  1145. EXPECT_EQ((80 + (i * 8)), p6->getLength());
  1146. }
  1147. }
  1148. // Goal of this test is to verify the a whole prefix can be delegated and that
  1149. // a whole subnet can be delegated.
  1150. TEST_F(Dhcp6ParserTest, subnetAndPrefixDelegated) {
  1151. ConstElementPtr x;
  1152. // Define a single valid pd pool.
  1153. string config =
  1154. "{ " + genIfaceConfig() + ","
  1155. "\"preferred-lifetime\": 3000,"
  1156. "\"rebind-timer\": 2000, "
  1157. "\"renew-timer\": 1000, "
  1158. "\"subnet6\": [ { "
  1159. " \"subnet\": \"2001:db8:1::/64\","
  1160. " \"pd-pools\": ["
  1161. " { \"prefix\": \"2001:db8:1::\", "
  1162. " \"prefix-len\": 64, "
  1163. " \"delegated-len\": 64"
  1164. " } ],"
  1165. "\"valid-lifetime\": 4000 }"
  1166. "] }";
  1167. // Convert the JSON string into Elements.
  1168. ElementPtr json;
  1169. ASSERT_NO_THROW(json = Element::fromJSON(config));
  1170. // Verify that DHCP6 configuration processing succeeds.
  1171. // Returned value must be non-empty ConstElementPtr to config result.
  1172. // rcode should be 0 which indicates successful configuration processing.
  1173. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1174. checkResult(x, 0);
  1175. // Test that we can retrieve the subnet.
  1176. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1177. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1178. ASSERT_TRUE(subnet);
  1179. // Fetch the collection of PD pools. It should have 1 entry.
  1180. PoolCollection pc;
  1181. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_PD));
  1182. EXPECT_EQ(1, pc.size());
  1183. // Get a pointer to the pd pool instance, and verify its contents.
  1184. Pool6Ptr p6;
  1185. ASSERT_NO_THROW(p6 = boost::dynamic_pointer_cast<Pool6>(pc[0]));
  1186. ASSERT_TRUE(p6);
  1187. EXPECT_EQ("2001:db8:1::", p6->getFirstAddress().toText());
  1188. EXPECT_EQ(64, p6->getLength());
  1189. // prefix-len is not directly accessible after pool construction, so
  1190. // verify that it was interpreted correctly by checking the last address
  1191. // value.
  1192. isc::asiolink::IOAddress prefixAddress("2001:db8:1::");
  1193. EXPECT_EQ(lastAddrInPrefix(prefixAddress, 64), p6->getLastAddress());
  1194. }
  1195. // Goal of this test is check for proper handling of invalid prefix delegation
  1196. // pool configuration. It uses an array of invalid configurations to check
  1197. // a variety of configuration errors.
  1198. TEST_F(Dhcp6ParserTest, invalidPdPools) {
  1199. ConstElementPtr x;
  1200. const char *config[] = {
  1201. // No prefix.
  1202. "{ \"interfaces-config\": { },"
  1203. "\"preferred-lifetime\": 3000,"
  1204. "\"rebind-timer\": 2000, "
  1205. "\"renew-timer\": 1000, "
  1206. "\"subnet6\": [ { "
  1207. " \"subnet\": \"2001:db8:1::/64\","
  1208. " \"pd-pools\": ["
  1209. " { "
  1210. " \"prefix-len\": 64, "
  1211. " \"delegated-len\": 128"
  1212. " } ],"
  1213. "\"valid-lifetime\": 4000 }"
  1214. "] }",
  1215. // No prefix-len.
  1216. "{ \"interfaces-config\": { },"
  1217. "\"preferred-lifetime\": 3000,"
  1218. "\"rebind-timer\": 2000, "
  1219. "\"renew-timer\": 1000, "
  1220. "\"subnet6\": [ { "
  1221. " \"subnet\": \"2001:db8:1::/64\","
  1222. " \"pd-pools\": ["
  1223. " { \"prefix\": \"2001:db8:1::\", "
  1224. " \"delegated-len\": 128"
  1225. " } ],"
  1226. "\"valid-lifetime\": 4000 }"
  1227. "] }",
  1228. // No delegated-len.
  1229. "{ \"interfaces-config\": { },"
  1230. "\"preferred-lifetime\": 3000,"
  1231. "\"rebind-timer\": 2000, "
  1232. "\"renew-timer\": 1000, "
  1233. "\"subnet6\": [ { "
  1234. " \"subnet\": \"2001:db8:1::/64\","
  1235. " \"pd-pools\": ["
  1236. " { \"prefix\": \"2001:db8:1::\", "
  1237. " \"prefix-len\": 64 "
  1238. " } ],"
  1239. "\"valid-lifetime\": 4000 }"
  1240. "] }",
  1241. // Delegated length is too short.
  1242. "{ \"interfaces-config\": { },"
  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:1::\", "
  1250. " \"prefix-len\": 128, "
  1251. " \"delegated-len\": 64"
  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 = "{ " + genIfaceConfig() + ","
  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 = "{ " + genIfaceConfig() + ","
  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 = "{ " + genIfaceConfig() + ","
  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 = "{ " + genIfaceConfig() + ","
  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 = "{ " + genIfaceConfig() + ","
  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 = "{ " + genIfaceConfig() + ","
  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 = "{ " + genIfaceConfig() + ","
  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 = "{ " + genIfaceConfig() + ","
  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 = "{ " + genIfaceConfig() + ","
  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-config\": { },"
  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-config\": {"
  2692. " \"interfaces\": [ \"eth0\" ]"
  2693. "},"
  2694. "\"preferred-lifetime\": 3000,"
  2695. "\"rebind-timer\": 2000, "
  2696. "\"renew-timer\": 1000, "
  2697. "\"valid-lifetime\": 4000 }";
  2698. ElementPtr json = Element::fromJSON(config);
  2699. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2700. // returned value must be 1 (values error)
  2701. // as the pool does not belong to that subnet
  2702. checkResult(status, 0);
  2703. CfgMgr::instance().getStagingCfg()->getCfgIface()->openSockets(AF_INET6, 10000);
  2704. // eth0 and eth1 were explicitly selected. eth2 was not.
  2705. EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET6));
  2706. EXPECT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  2707. }
  2708. // This test verifies that it is possible to configure the server to listen on
  2709. // all interfaces.
  2710. TEST_F(Dhcp6ParserTest, allInterfaces) {
  2711. IfaceMgrTestConfig test_config(true);
  2712. ASSERT_FALSE(test_config.socketOpen("eth0", AF_INET6));
  2713. ASSERT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  2714. ConstElementPtr status;
  2715. // This configuration specifies two interfaces on which server should listen
  2716. // but also includes '*'. This keyword switches server into the
  2717. // mode when it listens on all interfaces regardless of what interface names
  2718. // were specified in the "interfaces" parameter.
  2719. string config = "{ \"interfaces-config\": {"
  2720. " \"interfaces\": [ \"eth0\", \"eth1\", \"*\" ]"
  2721. "},"
  2722. "\"preferred-lifetime\": 3000,"
  2723. "\"rebind-timer\": 2000, "
  2724. "\"renew-timer\": 1000, "
  2725. "\"valid-lifetime\": 4000 }";
  2726. ElementPtr json = Element::fromJSON(config);
  2727. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2728. checkResult(status, 0);
  2729. CfgMgr::instance().getStagingCfg()->getCfgIface()->openSockets(AF_INET6, 10000);
  2730. // All interfaces should be now active.
  2731. EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET6));
  2732. EXPECT_TRUE(test_config.socketOpen("eth1", AF_INET6));
  2733. }
  2734. // This test checks if it is possible to specify relay information
  2735. TEST_F(Dhcp6ParserTest, subnetRelayInfo) {
  2736. ConstElementPtr status;
  2737. // A config with relay information.
  2738. string config = "{ " + genIfaceConfig() + ","
  2739. "\"rebind-timer\": 2000, "
  2740. "\"renew-timer\": 1000, "
  2741. "\"subnet6\": [ { "
  2742. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  2743. " \"relay\": { "
  2744. " \"ip-address\": \"2001:db8:1::abcd\""
  2745. " },"
  2746. " \"subnet\": \"2001:db8:1::/64\" } ],"
  2747. "\"preferred-lifetime\": 3000, "
  2748. "\"valid-lifetime\": 4000 }";
  2749. ElementPtr json = Element::fromJSON(config);
  2750. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2751. // returned value should be 0 (configuration success)
  2752. checkResult(status, 0);
  2753. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2754. selectSubnet(IOAddress("2001:db8:1::1"), classify_);
  2755. ASSERT_TRUE(subnet);
  2756. EXPECT_EQ("2001:db8:1::abcd", subnet->getRelayInfo().addr_.toText());
  2757. }
  2758. // Goal of this test is to verify that multiple subnets can be configured
  2759. // with defined client classes.
  2760. TEST_F(Dhcp6ParserTest, classifySubnets) {
  2761. ConstElementPtr x;
  2762. string config = "{ " + genIfaceConfig() + ","
  2763. "\"preferred-lifetime\": 3000,"
  2764. "\"rebind-timer\": 2000, "
  2765. "\"renew-timer\": 1000, "
  2766. "\"subnet6\": [ { "
  2767. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2768. " \"subnet\": \"2001:db8:1::/64\", "
  2769. " \"client-class\": \"alpha\" "
  2770. " },"
  2771. " {"
  2772. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  2773. " \"subnet\": \"2001:db8:2::/64\", "
  2774. " \"client-class\": \"beta\" "
  2775. " },"
  2776. " {"
  2777. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  2778. " \"subnet\": \"2001:db8:3::/64\", "
  2779. " \"client-class\": \"gamma\" "
  2780. " },"
  2781. " {"
  2782. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  2783. " \"subnet\": \"2001:db8:4::/64\" "
  2784. " } ],"
  2785. "\"valid-lifetime\": 4000 }";
  2786. ElementPtr json = Element::fromJSON(config);
  2787. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2788. checkResult(x, 0);
  2789. const Subnet6Collection* subnets =
  2790. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  2791. ASSERT_TRUE(subnets);
  2792. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  2793. // Let's check if client belonging to alpha class is supported in subnet[0]
  2794. // and not supported in any other subnet (except subnet[3], which allows
  2795. // everyone).
  2796. ClientClasses classes;
  2797. classes.insert("alpha");
  2798. EXPECT_TRUE (subnets->at(0)->clientSupported(classes));
  2799. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2800. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2801. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2802. // Let's check if client belonging to beta class is supported in subnet[1]
  2803. // and not supported in any other subnet (except subnet[3], which allows
  2804. // everyone).
  2805. classes.clear();
  2806. classes.insert("beta");
  2807. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2808. EXPECT_TRUE (subnets->at(1)->clientSupported(classes));
  2809. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2810. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2811. // Let's check if client belonging to gamma class is supported in subnet[2]
  2812. // and not supported in any other subnet (except subnet[3], which allows
  2813. // everyone).
  2814. classes.clear();
  2815. classes.insert("gamma");
  2816. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2817. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2818. EXPECT_TRUE (subnets->at(2)->clientSupported(classes));
  2819. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2820. // Let's check if client belonging to some other class (not mentioned in
  2821. // the config) is supported only in subnet[3], which allows everyone.
  2822. classes.clear();
  2823. classes.insert("delta");
  2824. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2825. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2826. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2827. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2828. // Finally, let's check class-less client. He should be allowed only in
  2829. // the last subnet, which does not have any class restrictions.
  2830. classes.clear();
  2831. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  2832. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  2833. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  2834. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  2835. }
  2836. // This test checks the ability of the server to parse a configuration
  2837. // containing a full, valid dhcp-ddns (D2ClientConfig) entry.
  2838. TEST_F(Dhcp6ParserTest, d2ClientConfig) {
  2839. ConstElementPtr status;
  2840. // Verify that the D2 configuraiton can be fetched and is set to disabled.
  2841. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2842. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  2843. // Verify that the convenience method agrees.
  2844. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  2845. string config_str = "{ " + genIfaceConfig() + ","
  2846. "\"preferred-lifetime\": 3000,"
  2847. "\"valid-lifetime\": 4000,"
  2848. "\"rebind-timer\": 2000, "
  2849. "\"renew-timer\": 1000, "
  2850. "\"subnet6\": [ { "
  2851. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2852. " \"subnet\": \"2001:db8:1::/64\" } ], "
  2853. " \"dhcp-ddns\" : {"
  2854. " \"enable-updates\" : true, "
  2855. " \"server-ip\" : \"3001::1\", "
  2856. " \"server-port\" : 777, "
  2857. " \"sender-ip\" : \"3001::2\", "
  2858. " \"sender-port\" : 778, "
  2859. " \"max-queue-size\" : 2048, "
  2860. " \"ncr-protocol\" : \"UDP\", "
  2861. " \"ncr-format\" : \"JSON\", "
  2862. " \"always-include-fqdn\" : true, "
  2863. " \"allow-client-update\" : true, "
  2864. " \"override-no-update\" : true, "
  2865. " \"override-client-update\" : true, "
  2866. " \"replace-client-name\" : true, "
  2867. " \"generated-prefix\" : \"test.prefix\", "
  2868. " \"qualifying-suffix\" : \"test.suffix.\" },"
  2869. "\"valid-lifetime\": 4000 }";
  2870. // Convert the JSON string to configuration elements.
  2871. ElementPtr config;
  2872. ASSERT_NO_THROW(config = Element::fromJSON(config_str));
  2873. // Pass the configuration in for parsing.
  2874. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config));
  2875. // check if returned status is OK
  2876. checkResult(status, 0);
  2877. // Verify that DHCP-DDNS updating is enabled.
  2878. EXPECT_TRUE(CfgMgr::instance().ddnsEnabled());
  2879. // Verify that the D2 configuration can be retrieved.
  2880. d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2881. ASSERT_TRUE(d2_client_config);
  2882. // Verify that the configuration values are correct.
  2883. EXPECT_TRUE(d2_client_config->getEnableUpdates());
  2884. EXPECT_EQ("3001::1", d2_client_config->getServerIp().toText());
  2885. EXPECT_EQ(777, d2_client_config->getServerPort());
  2886. EXPECT_EQ("3001::2", d2_client_config->getSenderIp().toText());
  2887. EXPECT_EQ(778, d2_client_config->getSenderPort());
  2888. EXPECT_EQ(2048, d2_client_config->getMaxQueueSize());
  2889. EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_client_config->getNcrProtocol());
  2890. EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_client_config->getNcrFormat());
  2891. EXPECT_TRUE(d2_client_config->getAlwaysIncludeFqdn());
  2892. EXPECT_TRUE(d2_client_config->getOverrideNoUpdate());
  2893. EXPECT_TRUE(d2_client_config->getOverrideClientUpdate());
  2894. EXPECT_TRUE(d2_client_config->getReplaceClientName());
  2895. EXPECT_EQ("test.prefix", d2_client_config->getGeneratedPrefix());
  2896. EXPECT_EQ("test.suffix.", d2_client_config->getQualifyingSuffix());
  2897. }
  2898. // This test checks the ability of the server to handle a configuration
  2899. // containing an invalid dhcp-ddns (D2ClientConfig) entry.
  2900. TEST_F(Dhcp6ParserTest, invalidD2ClientConfig) {
  2901. ConstElementPtr status;
  2902. // Configuration string with an invalid D2 client config,
  2903. // "server-ip" is invalid.
  2904. string config_str = "{ " + genIfaceConfig() + ","
  2905. "\"rebind-timer\": 2000, "
  2906. "\"renew-timer\": 1000, "
  2907. "\"subnet6\": [ { "
  2908. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2909. " \"subnet\": \"2001:db8:1::/64\" } ], "
  2910. " \"dhcp-ddns\" : {"
  2911. " \"enable-updates\" : true, "
  2912. " \"server-ip\" : \"bogus-value\", "
  2913. " \"server-port\" : 5301, "
  2914. " \"ncr-protocol\" : \"UDP\", "
  2915. " \"ncr-format\" : \"JSON\", "
  2916. " \"always-include-fqdn\" : true, "
  2917. " \"allow-client-update\" : true, "
  2918. " \"override-no-update\" : true, "
  2919. " \"override-client-update\" : true, "
  2920. " \"replace-client-name\" : true, "
  2921. " \"generated-prefix\" : \"test.prefix\", "
  2922. " \"qualifying-suffix\" : \"test.suffix.\" },"
  2923. "\"valid-lifetime\": 4000 }";
  2924. // Convert the JSON string to configuration elements.
  2925. ElementPtr config;
  2926. ASSERT_NO_THROW(config = Element::fromJSON(config_str));
  2927. // Configuration should not throw, but should fail.
  2928. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config));
  2929. // check if returned status is failed.
  2930. checkResult(status, 1);
  2931. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  2932. // Verify that the D2 configuraiton can be fetched and is set to disabled.
  2933. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  2934. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  2935. // Verify that the convenience method agrees.
  2936. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  2937. }
  2938. /// @brief Checks if the reservation is in the range of reservations.
  2939. ///
  2940. /// @param resrv Reservation to be searched for.
  2941. /// @param range Range of reservations returned by the @c Host object
  2942. /// in which the reservation will be searched.
  2943. bool reservationExists(const IPv6Resrv& resrv, const IPv6ResrvRange& range) {
  2944. for (IPv6ResrvIterator it = range.first; it != range.second;
  2945. ++it) {
  2946. if (resrv == it->second) {
  2947. return (true);
  2948. }
  2949. }
  2950. return (false);
  2951. }
  2952. // This test verifies that the host reservations can be specified for
  2953. // respective IPv6 subnets.
  2954. TEST_F(Dhcp6ParserTest, reservations) {
  2955. ConstElementPtr x;
  2956. string config = "{ " + genIfaceConfig() + ","
  2957. "\"rebind-timer\": 2000, "
  2958. "\"renew-timer\": 1000, "
  2959. "\"subnet6\": [ "
  2960. " { "
  2961. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2962. " \"subnet\": \"2001:db8:1::/64\", "
  2963. " \"id\": 123,"
  2964. " \"reservations\": ["
  2965. " ]"
  2966. " },"
  2967. " {"
  2968. " \"reservations\": ["
  2969. " {"
  2970. " \"duid\": \"01:02:03:04:05:06:07:08:09:0A\","
  2971. " \"ip-addresses\": [ \"2001:db8:2::1234\" ],"
  2972. " \"hostname\": \"\""
  2973. " },"
  2974. " {"
  2975. " \"hw-address\": \"01:02:03:04:05:06\","
  2976. " \"ip-addresses\": [ \"2001:db8:2::abcd\" ],"
  2977. " \"hostname\": \"\""
  2978. " }"
  2979. " ],"
  2980. " \"pools\": [ ],"
  2981. " \"subnet\": \"2001:db8:2::/64\", "
  2982. " \"id\": 234"
  2983. " },"
  2984. " {"
  2985. " \"pools\": [ ],"
  2986. " \"subnet\": \"2001:db8:3::/64\", "
  2987. " \"id\": 542,"
  2988. " \"reservations\": ["
  2989. " {"
  2990. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\","
  2991. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  2992. " \"hostname\": \"\""
  2993. " },"
  2994. " {"
  2995. " \"hw-address\": \"06:05:04:03:02:01\","
  2996. " \"prefixes\": [ \"2001:db8:3:1::/96\" ],"
  2997. " \"hostname\": \"\""
  2998. " }"
  2999. " ]"
  3000. " } "
  3001. "], "
  3002. "\"preferred-lifetime\": 3000,"
  3003. "\"valid-lifetime\": 4000 }";
  3004. ElementPtr json = Element::fromJSON(config);
  3005. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3006. checkResult(x, 0);
  3007. // Make sure all subnets have been successfully configured. There is no
  3008. // need to sanity check the subnet properties because it should have
  3009. // been already tested by other tests.
  3010. const Subnet6Collection* subnets =
  3011. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  3012. ASSERT_TRUE(subnets);
  3013. ASSERT_EQ(3, subnets->size());
  3014. // Hosts configuration must be available.
  3015. CfgHostsPtr hosts_cfg = CfgMgr::instance().getStagingCfg()->getCfgHosts();
  3016. ASSERT_TRUE(hosts_cfg);
  3017. // Let's create an object holding hardware address of the host having
  3018. // a reservation in the subnet having id of 234. For simlicity the
  3019. // address is a collection of numbers from 1 to 6.
  3020. std::vector<uint8_t> hwaddr_vec;
  3021. for (int i = 1; i < 7; ++i) {
  3022. hwaddr_vec.push_back(static_cast<uint8_t>(i));
  3023. }
  3024. HWAddrPtr hwaddr(new HWAddr(hwaddr_vec, HTYPE_ETHER));
  3025. // Retrieve the reservation and sanity check the address reserved.
  3026. ConstHostPtr host = hosts_cfg->get6(234, DuidPtr(), hwaddr);
  3027. ASSERT_TRUE(host);
  3028. IPv6ResrvRange resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
  3029. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3030. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_NA,
  3031. IOAddress("2001:db8:2::abcd")),
  3032. resrv));
  3033. // This reservation should be solely assigned to the subnet 234,
  3034. // and not to other two.
  3035. EXPECT_FALSE(hosts_cfg->get6(123, DuidPtr(), hwaddr));
  3036. EXPECT_FALSE(hosts_cfg->get6(542, DuidPtr(), hwaddr));
  3037. // Do the same test for the DUID based reservation.
  3038. std::vector<uint8_t> duid_vec;
  3039. for (int i = 1; i < 0xb; ++i) {
  3040. duid_vec.push_back(static_cast<uint8_t>(i));
  3041. }
  3042. DuidPtr duid(new DUID(duid_vec));
  3043. host = hosts_cfg->get6(234, duid);
  3044. ASSERT_TRUE(host);
  3045. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
  3046. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3047. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_NA,
  3048. IOAddress("2001:db8:2::1234")),
  3049. resrv));
  3050. EXPECT_FALSE(hosts_cfg->get6(123, duid));
  3051. EXPECT_FALSE(hosts_cfg->get6(542, duid));
  3052. // The HW address used for one of the reservations in the subnet 542
  3053. // consists of numbers from 6 to 1. So, let's just reverse the order
  3054. // of the address from the previous test.
  3055. hwaddr->hwaddr_.assign(hwaddr_vec.rbegin(), hwaddr_vec.rend());
  3056. host = hosts_cfg->get6(542, DuidPtr(), hwaddr);
  3057. EXPECT_TRUE(host);
  3058. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_PD);
  3059. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3060. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_PD,
  3061. IOAddress("2001:db8:3:1::"),
  3062. 96), resrv));
  3063. // This reservation must not belong to other subnets.
  3064. EXPECT_FALSE(hosts_cfg->get6(123, DuidPtr(), hwaddr));
  3065. EXPECT_FALSE(hosts_cfg->get6(234, DuidPtr(), hwaddr));
  3066. // Repeat the test for the DUID based reservation in this subnet.
  3067. duid.reset(new DUID(std::vector<uint8_t>(duid_vec.rbegin(),
  3068. duid_vec.rend())));
  3069. host = hosts_cfg->get6(542, duid);
  3070. ASSERT_TRUE(host);
  3071. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_PD);
  3072. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3073. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_PD,
  3074. IOAddress("2001:db8:3:2::"),
  3075. 96), resrv));
  3076. EXPECT_FALSE(hosts_cfg->get6(123, duid));
  3077. EXPECT_FALSE(hosts_cfg->get6(234, duid));
  3078. }
  3079. // This test verfies that the bogus host reservation would trigger a
  3080. // server configuration error.
  3081. TEST_F(Dhcp6ParserTest, reservationBogus) {
  3082. // Case 1: misspelled "duid" parameter.
  3083. ConstElementPtr x;
  3084. string config = "{ " + genIfaceConfig() + ","
  3085. "\"rebind-timer\": 2000, "
  3086. "\"renew-timer\": 1000, "
  3087. "\"subnet6\": [ "
  3088. " { "
  3089. " \"pools\": [ ],"
  3090. " \"subnet\": \"2001:db8:3::/64\", "
  3091. " \"id\": 542,"
  3092. " \"reservations\": ["
  3093. " {"
  3094. " \"dui\": \"0A:09:08:07:06:05:04:03:02:01\","
  3095. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  3096. " \"hostname\": \"\""
  3097. " }"
  3098. " ]"
  3099. " } "
  3100. "], "
  3101. "\"preferred-lifetime\": 3000,"
  3102. "\"valid-lifetime\": 4000 }";
  3103. ElementPtr json = Element::fromJSON(config);
  3104. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3105. checkResult(x, 1);
  3106. // Case 2: DUID and HW Address both specified.
  3107. config = "{ " + genIfaceConfig() + ","
  3108. "\"rebind-timer\": 2000, "
  3109. "\"renew-timer\": 1000, "
  3110. "\"subnet6\": [ "
  3111. " { "
  3112. " \"pools\": [ ],"
  3113. " \"subnet\": \"2001:db8:3::/64\", "
  3114. " \"id\": 542,"
  3115. " \"reservations\": ["
  3116. " {"
  3117. " \"hw-address\": \"01:02:03:04:05:06\","
  3118. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\","
  3119. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  3120. " \"hostname\": \"\""
  3121. " }"
  3122. " ]"
  3123. " } "
  3124. "], "
  3125. "\"preferred-lifetime\": 3000,"
  3126. "\"valid-lifetime\": 4000 }";
  3127. json = Element::fromJSON(config);
  3128. // Remove existing configuration, if any.
  3129. CfgMgr::instance().clear();
  3130. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3131. checkResult(x, 1);
  3132. // Case 3: Neither ip address nor hostname specified.
  3133. config = "{ " + genIfaceConfig() + ","
  3134. "\"rebind-timer\": 2000, "
  3135. "\"renew-timer\": 1000, "
  3136. "\"subnet6\": [ "
  3137. " { "
  3138. " \"pools\": [ ],"
  3139. " \"subnet\": \"2001:db8:3::/64\", "
  3140. " \"id\": 542,"
  3141. " \"reservations\": ["
  3142. " {"
  3143. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\""
  3144. " }"
  3145. " ]"
  3146. " } "
  3147. "], "
  3148. "\"preferred-lifetime\": 3000,"
  3149. "\"valid-lifetime\": 4000 }";
  3150. json = Element::fromJSON(config);
  3151. // Remove existing configuration, if any.
  3152. CfgMgr::instance().clear();
  3153. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3154. checkResult(x, 1);
  3155. }
  3156. /// The goal of this test is to verify that configuration can include
  3157. /// MAC/Hardware sources. This test also checks if the aliases are
  3158. /// handled properly (rfc6939 = client-addr-relay, rfc4649 = remote-id,
  3159. /// rfc4580 = subscriber-id).
  3160. TEST_F(Dhcp6ParserTest, macSources) {
  3161. ConstElementPtr status;
  3162. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3163. Element::fromJSON("{ " + genIfaceConfig() + ","
  3164. "\"mac-sources\": [ \"rfc6939\", \"rfc4649\", \"rfc4580\","
  3165. "\"client-link-addr-option\", \"remote-id\", \"subscriber-id\"],"
  3166. "\"preferred-lifetime\": 3000,"
  3167. "\"rebind-timer\": 2000, "
  3168. "\"renew-timer\": 1000, "
  3169. "\"subnet6\": [ ], "
  3170. "\"valid-lifetime\": 4000 }")));
  3171. // returned value should be 0 (success)
  3172. checkResult(status, 0);
  3173. CfgMACSources mac_sources = CfgMgr::instance().getStagingCfg()->getMACSources().get();
  3174. ASSERT_EQ(6, mac_sources.size());
  3175. // Let's check the aliases. They should be recognized to their base methods.
  3176. EXPECT_EQ(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION, mac_sources[0]);
  3177. EXPECT_EQ(HWAddr::HWADDR_SOURCE_REMOTE_ID, mac_sources[1]);
  3178. EXPECT_EQ(HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID, mac_sources[2]);
  3179. // Let's check if the actual methods are recognized properly.
  3180. EXPECT_EQ(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION, mac_sources[3]);
  3181. EXPECT_EQ(HWAddr::HWADDR_SOURCE_REMOTE_ID, mac_sources[4]);
  3182. EXPECT_EQ(HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID, mac_sources[5]);
  3183. }
  3184. /// The goal of this test is to verify that MAC sources configuration can be
  3185. /// empty.
  3186. TEST_F(Dhcp6ParserTest, macSourcesEmpty) {
  3187. ConstElementPtr status;
  3188. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3189. Element::fromJSON("{ " + genIfaceConfig() + ","
  3190. "\"mac-sources\": [ ],"
  3191. "\"preferred-lifetime\": 3000,"
  3192. "\"rebind-timer\": 2000, "
  3193. "\"renew-timer\": 1000, "
  3194. "\"subnet6\": [ ], "
  3195. "\"valid-lifetime\": 4000 }")));
  3196. // returned value should be 0 (success)
  3197. checkResult(status, 0);
  3198. CfgMACSources mac_sources = CfgMgr::instance().getStagingCfg()->getMACSources().get();
  3199. EXPECT_EQ(0, mac_sources.size());
  3200. }
  3201. /// The goal of this test is to verify that MAC sources configuration can
  3202. /// only use valid parameters.
  3203. TEST_F(Dhcp6ParserTest, macSourcesBogus) {
  3204. ConstElementPtr status;
  3205. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3206. Element::fromJSON("{ " + genIfaceConfig() + ","
  3207. "\"mac-sources\": [ \"from-wire\" ],"
  3208. "\"preferred-lifetime\": 3000,"
  3209. "\"rebind-timer\": 2000, "
  3210. "\"renew-timer\": 1000, "
  3211. "\"subnet6\": [ ], "
  3212. "\"valid-lifetime\": 4000 }")));
  3213. // returned value should be 1 (failure)
  3214. checkResult(status, 1);
  3215. }
  3216. /// The goal of this test is to verify that Host Reservation modes can be
  3217. /// specified on a per-subnet basis.
  3218. TEST_F(Dhcp6ParserTest, hostReservationPerSubnet) {
  3219. /// - Configuration:
  3220. /// - only addresses (no prefixes)
  3221. /// - 4 subnets with:
  3222. /// - 2001:db8:1::/64 (all reservations enabled)
  3223. /// - 2001:db8:2::/64 (out-of-pool reservations)
  3224. /// - 2001:db8:3::/64 (reservations disabled)
  3225. /// - 2001:db8:3::/64 (reservations not specified)
  3226. const char* HR_CONFIG =
  3227. "{"
  3228. "\"preferred-lifetime\": 3000,"
  3229. "\"rebind-timer\": 2000, "
  3230. "\"renew-timer\": 1000, "
  3231. "\"subnet6\": [ { "
  3232. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  3233. " \"subnet\": \"2001:db8:1::/48\", "
  3234. " \"reservation-mode\": \"all\""
  3235. " },"
  3236. " {"
  3237. " \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  3238. " \"subnet\": \"2001:db8:2::/48\", "
  3239. " \"reservation-mode\": \"out-of-pool\""
  3240. " },"
  3241. " {"
  3242. " \"pools\": [ { \"pool\": \"2001:db8:3::/64\" } ],"
  3243. " \"subnet\": \"2001:db8:3::/48\", "
  3244. " \"reservation-mode\": \"disabled\""
  3245. " },"
  3246. " {"
  3247. " \"pools\": [ { \"pool\": \"2001:db8:4::/64\" } ],"
  3248. " \"subnet\": \"2001:db8:4::/48\" "
  3249. " } ],"
  3250. "\"valid-lifetime\": 4000 }";
  3251. ConstElementPtr status;
  3252. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3253. Element::fromJSON(HR_CONFIG)));
  3254. // returned value should be 0 (success)
  3255. checkResult(status, 0);
  3256. CfgMgr::instance().commit();
  3257. // Let's get all subnets and check that there are 4 of them.
  3258. ConstCfgSubnets6Ptr subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6();
  3259. ASSERT_TRUE(subnets);
  3260. const Subnet6Collection* subnet_col = subnets->getAll();
  3261. ASSERT_EQ(4, subnet_col->size()); // We expect 4 subnets
  3262. // Let's check if the parsed subnets have correct HR modes.
  3263. // Subnet 1
  3264. Subnet6Ptr subnet;
  3265. subnet = subnets->selectSubnet(IOAddress("2001:db8:1::1"));
  3266. ASSERT_TRUE(subnet);
  3267. EXPECT_EQ(Subnet::HR_ALL, subnet->getHostReservationMode());
  3268. // Subnet 2
  3269. subnet = subnets->selectSubnet(IOAddress("2001:db8:2::1"));
  3270. ASSERT_TRUE(subnet);
  3271. EXPECT_EQ(Subnet::HR_OUT_OF_POOL, subnet->getHostReservationMode());
  3272. // Subnet 3
  3273. subnet = subnets->selectSubnet(IOAddress("2001:db8:3::1"));
  3274. ASSERT_TRUE(subnet);
  3275. EXPECT_EQ(Subnet::HR_DISABLED, subnet->getHostReservationMode());
  3276. // Subnet 4
  3277. subnet = subnets->selectSubnet(IOAddress("2001:db8:4::1"));
  3278. ASSERT_TRUE(subnet);
  3279. EXPECT_EQ(Subnet::HR_ALL, subnet->getHostReservationMode());
  3280. }
  3281. /// The goal of this test is to verify that configuration can include
  3282. /// Relay Supplied options (specified as numbers).
  3283. TEST_F(Dhcp6ParserTest, rsooNumbers) {
  3284. ConstElementPtr status;
  3285. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3286. Element::fromJSON("{ " + genIfaceConfig() + ","
  3287. "\"relay-supplied-options\": [ \"10\", \"20\", \"30\" ],"
  3288. "\"preferred-lifetime\": 3000,"
  3289. "\"rebind-timer\": 2000, "
  3290. "\"renew-timer\": 1000, "
  3291. "\"subnet6\": [ ], "
  3292. "\"valid-lifetime\": 4000 }")));
  3293. // returned value should be 0 (success)
  3294. checkResult(status, 0);
  3295. // The following codes should be enabled now
  3296. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(10));
  3297. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(20));
  3298. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(30));
  3299. // This option is on the IANA list, so it should be allowed all the time
  3300. // (http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml)
  3301. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3302. ->enabled(D6O_ERP_LOCAL_DOMAIN_NAME));
  3303. // Those options are not enabled
  3304. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(25));
  3305. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(1));
  3306. }
  3307. /// The goal of this test is to verify that configuration can include
  3308. /// Relay Supplied options (specified as names).
  3309. TEST_F(Dhcp6ParserTest, rsooNames) {
  3310. ConstElementPtr status;
  3311. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3312. Element::fromJSON("{ " + genIfaceConfig() + ","
  3313. "\"relay-supplied-options\": [ \"dns-servers\", \"remote-id\" ],"
  3314. "\"preferred-lifetime\": 3000,"
  3315. "\"rebind-timer\": 2000, "
  3316. "\"renew-timer\": 1000, "
  3317. "\"subnet6\": [ ], "
  3318. "\"valid-lifetime\": 4000 }")));
  3319. // returned value should be 0 (success)
  3320. checkResult(status, 0);
  3321. for (uint16_t code = 0; code < D6O_NAME_SERVERS; ++code) {
  3322. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3323. ->enabled(code)) << " for option code " << code;
  3324. }
  3325. // The following code should be enabled now
  3326. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3327. ->enabled(D6O_NAME_SERVERS));
  3328. for (uint16_t code = D6O_NAME_SERVERS + 1; code < D6O_REMOTE_ID; ++code) {
  3329. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3330. ->enabled(code)) << " for option code " << code;
  3331. }
  3332. // Check remote-id. It should be enabled.
  3333. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3334. ->enabled(D6O_REMOTE_ID));
  3335. for (uint16_t code = D6O_REMOTE_ID + 1; code < D6O_ERP_LOCAL_DOMAIN_NAME; ++code) {
  3336. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3337. ->enabled(code)) << " for option code " << code;
  3338. }
  3339. // This option is on the IANA list, so it should be allowed all the time
  3340. // (http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml)
  3341. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3342. ->enabled(D6O_ERP_LOCAL_DOMAIN_NAME));
  3343. for (uint16_t code = D6O_ERP_LOCAL_DOMAIN_NAME + 1; code < 300; ++code) {
  3344. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3345. ->enabled(code)) << " for option code " << code;
  3346. }
  3347. }
  3348. TEST_F(Dhcp6ParserTest, rsooNegativeNumber) {
  3349. ConstElementPtr status;
  3350. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3351. Element::fromJSON("{ " + genIfaceConfig() + ","
  3352. "\"relay-supplied-options\": [ \"80\", \"-2\" ],"
  3353. "\"preferred-lifetime\": 3000,"
  3354. "\"rebind-timer\": 2000, "
  3355. "\"renew-timer\": 1000, "
  3356. "\"subnet6\": [ ], "
  3357. "\"valid-lifetime\": 4000 }")));
  3358. // returned value should be 0 (success)
  3359. checkResult(status, 1);
  3360. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3361. }
  3362. TEST_F(Dhcp6ParserTest, rsooBogusName) {
  3363. ConstElementPtr status;
  3364. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3365. Element::fromJSON("{ " + genIfaceConfig() + ","
  3366. "\"relay-supplied-options\": [ \"bogus\", \"dns-servers\" ],"
  3367. "\"preferred-lifetime\": 3000,"
  3368. "\"rebind-timer\": 2000, "
  3369. "\"renew-timer\": 1000, "
  3370. "\"subnet6\": [ ], "
  3371. "\"valid-lifetime\": 4000 }")));
  3372. // returned value should be 0 (success)
  3373. checkResult(status, 1);
  3374. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3375. }
  3376. };