config_parser_unittest.cc 122 KB

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