config_parser_unittest.cc 120 KB

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